python 设置默认字符集

(一)设置idle的字符串集问题python

进入IDLE命令行时咱们能够用ide

import sysspa

sys.getdefaultencoding()命令行

来查看环境中默认的字符集ip

可是不能调用 utf-8

sys.setdefaultencoding("utf-8")rem

在python文档中是这样描述的:文档

This function is only intended to be used by the site module implementation and, where needed, by sitecustomize. Once used by the site module, it is removed from the sys module’s namespace.字符串

也就是说只有在启动过程当中能够掉用,get

解决方法

一、利用sitecustomize.py

在 Python27\Lib\site-packages 目录下新增 sitecustomize.py

在文件中加入

import sys

sys.setdefaultencoding("utf-8")

二、利用reload

reload(sys)

sys.setdefaultencoding('utf-8')

此方法在命令行方式下致使打印输出的异常,


(二) 检查一个字符串的字符集

一、使用chardet安装包

(1)安装

pip install chardet

(2)使用

import chardet

chardet.detect(" 检查一个字符串的字符集");

相关文章
相关标签/搜索