Python views the encoding format of the file

  • 2020-06-19 10:42:45
  • OfStack

In the case of reading Chinese, there are usually some coding problems, but first you need to know what the current encoding is, and then use decode or encode to encode and decode. Here is how to use the chardet library to see the encoding.


import chardet
path = "E:/t.csv"
#path = "E:/t.zip"
f = open(path,'rb')
data = f.read()
print(chardet.detect(data))

The print results are as follows:

{'encoding': 'GB2312', 'confidence': 0.99, 'language': 'Chinese'}


Related articles: