python的中文文件打开方式‘utf-8’

关于中文的文本,读取总是会失败

Traceback (most recent call last):

File "F:\代码\python\学习练习\py_study\three.py", line 3, in <module>

txt = open("三国演义.txt", "r", encoding='utf-8').read()

File "C:\Python\lib\codecs.py", line 321, in decode

(result, consumed) = self._buffer_decode(data, self.errors, final)

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa1 in position 0: invalid start byte

出现这段报错。

这时候只需要将TXT文件另存为,编码方式改为‘utf-8’即可