且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用通用编码检测器(chardet)在Python中的文本文件中进行字符检测

更新时间:2023-02-20 10:56:04

chardet.detect 返回一个字典,键'encoding'。所以你可以这样做:

chardet.detect returns a dictionary which provides the encoding as the value associated with the key 'encoding'. So you can do this:

import chardet    
rawdata = open(infile, "r").read()
result = chardet.detect(rawdata)
charenc = result['encoding']