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

Python (225) 2023-07-29 21:35:36

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

THE END

发表回复