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']