1. 程式人生 > >python 檢視當前字串的編碼格式

python 檢視當前字串的編碼格式

1. chardet模組

The easiest way to use the Universal Encoding Detector library is with the detect function.
使用通用編碼檢測器庫的最簡單方法是使用detect函式
The detect function takes one argument, a non-Unicode string. It returns a dictionary containing the auto-detected character encoding and a confidence level from 0 to 1.
detect函式有一個引數, 即非 unicode 字串。它返回一個字典, 其中包含自動檢測的字元編碼和從0到1的置信度。

舉例

>>> import chardet
>>> chardet.detect('abc123')
{'confidence': 1.0, 'encoding': 'ascii'}
>>> chardet.detect('中國')
{'confidence': 0.7525, 'encoding': 'utf-8'}

2. Supported encoding for python