Python gets the encoding format of the file

Many times, we need to use python to open csv, txt, excel and other files, but sometimes we don’t know the encoding method of the file, and the encoding format will be wrong. The following code will help you solve this problem
#导入解析编码的包
import chardet

#城市表的路径
path = "D:/城市表.csv"
#打开文件
f = open(path,'rb')
#读取文件的方法,返回的是文件的数据
data = f.read()
#查看文件的编码格式
print(chardet.detect(data))

Guess you like

Origin blog.csdn.net/qq_38220334/article/details/106475508