【发布时间】:2019-11-11 01:59:35
【问题描述】:
您好,我使用 Allen Downey 的 oreilly 书学习 Python3.x。在第 9 章中有一个例子来处理 Moby 项目文件中的单词列表。
https://en.wikipedia.org/wiki/Moby_Project
https://web.archive.org/web/20170930060409/http://icon.shef.ac.uk/Moby/
我使用以下 Python 行阅读了 German.txt 文件。
with open("german.txt") as log:
for line in log:
word = line.strip()
if len(word) > 20:
print(word)
读了一些单词,但出现了中断,我得到了这些行。
Amtsueberschreitungen
Traceback (most recent call last):
File "einlesen.py", line 8, in <module>
for line in log:
File "/home/alexander/anaconda3/lib/python3.6/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x82 in position 394: invalid start byte
符号是什么意思?如何使用 python 代码处理这个问题。
谢谢
【问题讨论】:
-
您的文件采用哪种编码方式? Python 默认读取
UTF-8。看起来,像一个编码问题。 -
@albert,显然该文件被编码为 Mac OS 罗马编码(见下面我的answer)
标签: python python-3.x character-encoding