问题
在with open(path,'r') as f
的时候出现:
或者在with open(path,'r',encoding='utf-8') as f
等等,最好的方法是先判断一下文件的编码,然后‘对症下药’
解决办法
直接丢一个函数先判断一下文件的编码方式:
import chardet
def get_file_encoding(path):
with open(path,'rb') as f:
text = f.read()
file_encoding = chardet.detect(text)['encoding']
return file_encoding
然后在真正读取文件的时候指定该函数返回的编码为open的编码方式
open(path,'rb',encoding=get_file_encoding(path)) as f:
f# 问题
在`with open(path,'r') as f`的时候出现:

或者在`with open(path,'r',encoding='utf-8') as f`

Wait, the best way is to judge the encoding of the file first, and then 'get it right'
# Workaround
Drop a function directly to judge the encoding of the file first:
```python
import chardet
def get_file_encoding(path):
with open(path,'rb') as f:
text = f.read()
file_encoding = chardet.detect(text)['encoding']
return file_encoding
Then specify the encoding returned by the function as open encoding when the file is actually read
open(path,'rb',encoding=get_file_encoding(path)) as f:
...
成功解决!