【问题标题】:codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 318: ordinal not in range(128)codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 318: ordinal not in range(128)
【发布时间】: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


【解决方案1】:

@AndriiAbramamov 是对的,您应该检查该问题,这是一种打开文件的方法,该文件也在该链接上

import codecs
f = codecs.open('words.txt', 'r', 'UTF-8')
for line in f:
    print(line)

另一种方法是使用正则表达式,因此当您打开文件时,您可以删除任何特殊字符,如双引号等。

【讨论】:

    【解决方案2】:

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

    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)
    

    你必须精确地给出编码参数。

    【讨论】:

      【解决方案3】:

      你也可以尝试编码:

      with open(file) as f:
          for line in f: 
               line = line.encode('ascii','ignore').decode('UTF-8','ignore')
               print(line)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-31
        • 2018-08-25
        • 2018-11-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多