dti*_*idy 3 python ascii decode readlines
我试图打开并读取包含大量文本的.txt文件.下面是我的代码,我不知道如何解决这个问题.任何帮助将非常感激.
file = input("Please enter a .txt file: ")
myfile = open(file)
x = myfile.readlines()
print (x)
Run Code Online (Sandbox Code Playgroud)
当我输入.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)
Run Code Online (Sandbox Code Playgroud)
我没有使用编解码器,而是这样解决:
def test():
path = './test.log'
file = open(path, 'r+', encoding='utf-8')
while True:
lines = file.readlines()
if not lines:
break
for line in lines:
print(line)
Run Code Online (Sandbox Code Playgroud)
你必须准确地给出编码参数.
| 归档时间: |
|
| 查看次数: |
7046 次 |
| 最近记录: |