首页 > 文章列表 > python如何判断字符串编码方式

python如何判断字符串编码方式

Python 字符串 编码方式
205 2022-08-07

python判断字符串编码方式的方法:1、打开命令提示符,进入Python27\Script目录;2、执行命令安装chardet;3、判断字符串编码方式即可,如:【chardet.detect(f.read())】。

具体方法:

(推荐教程:Python入门教程)

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'} ,只能判断是否为某种编码的概率。比较准确的结果了。