首页 > 解决方案 > 以 utf-8 编码的文本文件,Python 给出 UnicodeDecodeError,忽略错误不起作用

问题描述

我正在尝试读入以 utf-8 编码的“CDPQ17CEO.txt”,请参阅此图像: Notepad++ Encoding

这是 read_in 函数(在 Letter 类中):

class Letter(object):

def __init__(self, file_path, company_name, author_name=None, author_type = None):
   self.letter = self._read_in(file_path)
   self.company = company_name
   self.author = author_name
   self.type = author_type

def _read_in(self, file_path):
    f = open(file_path, 'r', encoding='utf-8', errors='ignore').readlines()
    f_stripped = [line.strip() for line in f]
    f.close()
    return ' '.join(f_stripped)

这是函数调用:

full_file = 'Q:\My Documents\OTPP\letters\CDPQ17CEO.txt'    
letter_dict[name]=px.Letter(full_file, name, author_type=author_type)

这是错误:

UnicodeDecodeError:“charmap”编解码器无法解码位置 1936 中的字节 0x9d:字符映射到未定义>

为什么 errors = 'ignore' 不做它的工作?

如果我打开文本文档并将其转换为 ANSI,重新保存并重新运行,这确实有效,但我宁愿避免对我需要读入的所有文档执行此操作。

谢谢!

标签: pythonutf-8built-inpython-unicode

解决方案


问题及解决方案:

  • 包含 Letter 类的 px 模块实际上并未导入,尽管它似乎是
  • 通过将模块的路径添加到 PYTHONPATH 解决了问题

    import sys
    sys.path.append('foo')
    

推荐阅读