gpt4 book ai didi

python - codecs.ascii_decode(输入,self.errors)[0] UnicodeDecodeError : 'ascii' codec can't decode byte 0xc2 in position 318: ordinal not in range(128)

转载 作者:太空宇宙 更新时间:2023-11-03 13:34:48 33 4
gpt4 key购买 nike

我正在尝试打开并读取包含大量文本的 .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)

最佳答案

我没有使用编解码器,而是这样解决的:

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)

您必须准确地给出编码参数。

关于python - codecs.ascii_decode(输入,self.errors)[0] UnicodeDecodeError : 'ascii' codec can't decode byte 0xc2 in position 318: ordinal not in range(128),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41512427/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com