运行代码:
with open('F:\dataanalysis\paragraph.txt', encoding='utf-8') as file:
text=file.read()
print(text)
错误提示:
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-52-421bbf0f9b6c> in <module>()
1 with open('F:\dataanalysis\paragraph.txt', encoding='utf-8') as file:
2 # with open('F:\dataanalysis\paragraph.txt', encoding='unicode_escape') as file:
----> 3 text=file.read()
4 print(text)
D:\Anaconda\lib\codecs.py in decode(self, input, final)
320 # decode input (taking the buffer into account)
321 data = self.buffer + input
--> 322 (result, consumed) = self._buffer_decode(data, self.errors, final)
323 # keep undecoded input until the next call
324 self.buffer = data[consumed:]
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 0: invalid start byte
解决办法:
将paragraph.txt文件打开另存为,保存时修改文档编码为utf-8: