1. 安装chardet

    在命令行中,进入Python27\Scripts目录,输入以下的命令:easy_install chardet

2. 操作

import chardet
f = open('file','r')
fencoding=chardet.detect(f.read())
print fencoding

fencoding输出格式 {'confidence': 0.96630842899499614, 'encoding': 'GB2312'} ,只能判断是否为某种编码的概率。比较准确的结果了。输入参数为str类型。

备注:

  • str利用decode方法根据str的编码将其解码为unicode字符串类型
  • str利用encode根据特定的编码将unicode字符串类型转换为特定的编码

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-16
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-18
  • 2022-12-23
  • 2022-02-17
  • 2021-12-09
  • 2021-09-12
相关资源
相似解决方案