【发布时间】:2017-05-21 14:43:06
【问题描述】:
我正在尝试打开并读取包含大量文本的 .txt 文件。下面是我的代码,我不知道如何解决这个问题。任何帮助将不胜感激。
file = input("Please enter a .txt file: ")
myfile = open(file)
x = myfile.readlines()
print (x)
当我输入 .txt 文件时,下面会显示完整的错误消息:
line 10, in <module> x = myfile.readlines()
line 26, in decode return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 318: ordinal not in range(128)
【问题讨论】:
-
@AndriiAbramov 在那个问题中很明显该文件是 UTF-8 编码的。我不确定这里的情况是否如此,否则会导致很多挫败感。
标签: python ascii decode readlines