我正在用Python编写一些邮件处理软件,它在头字段中遇到奇怪的字节。
我正在寻找一个函数,它str
可以提供一些提示,并且可以提供一些提示,并且可以让我回馈一个提示unicode
。
你可以使用以下命令轻松安装它:
$ pip install chardet
示例代码如下:
>>> import chardet
>>> import urllib
>>> detect = lambda url: chardet.detect(urllib.urlopen(url).read())
>>> detect('http://stackoverflow.com')
{'confidence': 0.85663169917190185, 'encoding': 'ISO-8859-2'}
>>> detect('https://stackoverflow.com/questions/269060/is-there-a-python-lib')
{'confidence': 0.98999999999999999, 'encoding': 'utf-8'}