python - 以 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,重新保存并重新运行,这确实有效,但我宁愿避免对我需要读入的所有文档执行此操作。
谢谢!
解决方案
问题及解决方案:
- 包含 Letter 类的 px 模块实际上并未导入,尽管它似乎是
通过将模块的路径添加到 PYTHONPATH 解决了问题
import sys sys.path.append('foo')
推荐阅读
- php - laravel 和 laravel 宅基地有什么区别
- javascript - 网页转换策略
- vb.net - 如何通过单元格单击事件以另一种形式显示数据网格视图行值,而不将 show 方法放在单元格单击事件中
- arrays - TableViewCell 删除然后所有复选标记迅速消失
- java - 如何在 Activity 中运行 Fragment?
- nsis - 如何使用本地文件夹中的 NSIS 脚本添加新页面以创建安装程序,而不是从程序文件 /NSIS 中获取它
- android - React Native Android Unable to resolve all files 错误
- shopify - 如何访问或创建 Shopify Storefront 令牌 [403 Forbidden]?
- javascript - Socket.io / Node.js:无法运行 > 节点 server.js
- javascript - 重复 XML 元素在 Internet Explorer 中不起作用