当前位置: 首页 > 图灵资讯 > 行业资讯> python如何判断字符串编码方式

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

来源:图灵python
时间: 2025-01-08 14:57:22

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

具体方法:

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

1、安装chardet

在命令行中,进入Python27\Scripts目录,执行以下命令

easy_installchardet

2、判断方法

importchardet
f=open('file','r')
fencoding=chardet.detect(f.read())
printfencoding

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