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(" 檢查一個字符串的字符集");

相關文章
相關標籤/搜索