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

标签 python ascii decode readlines

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

相关文章:

algorithm - LZW 压缩似乎无法正常工作

python - 删除 csv 文件中的非 ascii 字符

python - 如何将 numpy 数组与 pandas 系列相乘?

java - 需要将字节数组/二进制消息从 UDP DatagramPacket 转换为 java 中的几个字段

java - ASCII-错误消息,找不到符号

c++ - 扩展 ASCII 字符 C++

c - 使用 avcodec_decode_audio4() 解码 AAC 时出错

android - BitmapFactory.decodeResource() 忽略 jpg 图像的 inPreferredConfig 选项

python - Django:创建版本号增加的模型对象

python - 如何在 Jupyter Notebook 的引擎上正确导入模块以进行并行处理?