【问题标题】:Python: UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)Python:UnicodeDecodeError:'ascii'编解码器无法解码位置 0 的字节 0xef:序数不在范围内(128)
【发布时间】:2017-03-30 00:59:43
【问题描述】:

我目前的 python 3 代码有问题。

 replace_line('Products.txt', line, tenminus_str)

是我试图转换为 utf-8 的行,但是当我尝试像对待其他人一样执行此操作时,我会收到诸如无属性错误之类的错误,并且当我尝试添加时,例如...

.decode("utf8")

...到最后,我仍然收到它使用 ascii 的错误。我还尝试了与其他行一起使用的其他方法,例如添加 io.在前面并用

添加逗号
encoding = 'utf8'

我用于 replace_line 的函数是:

def replace_line(file_name, line_num, text):
    lines = open(file_name, 'r').readlines()
    lines[line_num] = text
    out = open(file_name, 'w')
    out.writelines(lines)
    out.close()

我该如何解决这个问题?请注意,我对 Python 很陌生,而且还不够先进,无法很好地进行调试。

编辑:这个问题的解决方法不同于“重复”

编辑 2:我现在有另一个函数错误。

File "FILELOCATION", line 45, in refill replace_line('Products.txt', str(line), tenminus_str) 

File "FILELOCATION", line 6, in replace_line lines[line_num] = text

TypeError: list indices must be integers, not str 

这是什么意思,我该如何解决?

【问题讨论】:

  • 向我们展示您的 stracktrace,向我们展示您的数据
  • 什么意思?
  • 使用 utf_8_sig,而不是 utf8,您的文件可能以 bom 开头
  • .decode('utf_8_sig')
  • decode('utf8', errors='ignore')

标签: python unicode utf-8 ascii


【解决方案1】:

处理编码问题您可以尝试将以下设置添加到您的脑海中


import sys
reload(sys)
sys.setdefaultencoding('utf-8')
Type = sys.getfilesystemencoding()

【讨论】:

【解决方案2】:

codecs 模块正是您所需要的。 detail这里

import codecs
def replace_line(file_name, line_num, text):
    f = codecs.open(file_name, 'r', encoding='utf-8')
    lines = f.readlines()
    lines[line_num] = text
    f.close()
    w = codecs.open(file_name, 'w', encoding='utf-8')
    w.writelines(lines)
    w.close()

【讨论】:

  • Python 3 中不需要codecsopen 也支持encoding 参数。
  • @MarkRansom 感谢您的指出。实际上,我是 python2 的开发者... :)
  • 我现在有另一个函数错误。 File "LOCATION", line 45, in refill replace_line('Products.txt', str(line), tenminus_str) File "LOCATION", line 6, in replace_line lines[line_num] = text TypeError: list indices must be integers, not str 这是什么意思,我该如何解决?
  • @NamenotFound 第二个参数line_num应该是一个整数,代表你需要替换哪一行。您已将 str 传递给该函数,因此它将因该错误而失败。你应该调用像replace_line("Products.txt", 1, tenminus_str)这样的函数,这意味着你想用字符串tenminus_str替换第二行。
  • 在 Python 2 中也不需要 codecsio.open 存在于 Python 2 和 Python 3 中,其工作方式类似于 Python 3 的内置 open
【解决方案3】:

将您的功能更改为:

def replace_line(file_name, line_num, text):
    with open(file_name, 'r', encoding='utf8') as f:
        lines = f.readlines()
    lines[line_num] = text
    with open(file_name, 'w', encoding='utf8') as out:
        out.writelines(lines)

encoding='utf8' 将正确解码您的 UTF-8 文件。

with 在退出块时自动关闭文件。

由于您的文件以 \xef 开头,因此它的开头可能有一个 UTF-8 编码字节顺序标记 (BOM) 字符。上面的代码将在输出时保持这一点,但如果您不希望它使用 utf-8-sig 进行 input 编码。然后它会被自动删除。

【讨论】:

    【解决方案4】:

    如果您正在读取文件,请尝试添加 encoding='utf8'

    with open("../file_path", encoding='utf8'):
             # your code
    

    【讨论】:

      猜你喜欢
      • 2014-08-12
      • 2018-01-11
      • 1970-01-01
      • 2021-08-08
      • 2011-05-13
      • 2014-02-19
      • 2018-07-26
      • 2020-11-06
      • 2013-09-20
      相关资源
      最近更新 更多