在Python中使用chardet检测文件字符编码方式

Python 中的chardet模块中只有一个函数detect。

detect函数的参数要求是bytes类型。

我们可以通过读取网页内容、open函数的rb模式、带b前缀的字符串、encode函数等途径获得bytes类型的数据。

Code :


import chardet

file_path=r'./XXXX.csv'

with open(file_path,'rb') as f:
    file_encoding=chardet.detect(f.read())['encoding']   


版权声明:本文为qq_40728667原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。