【问题标题】:python charmap codec can't decode byte X in position Y character maps to <undefined>python Charmap编解码器无法解码位置Y字符映射到<undefined>中的字节X
【发布时间】:2017-08-12 15:28:15
【问题描述】:

我正在尝试使用 python 库进行数据分析,我面临的问题是这个异常

用户代码未处理 UnicodeDecodeError 消息:“charmap”编解码器 无法解码位置 165 中的字节 0x81:字符映射到

我已经查看了类似问题的答案,并且 OP 似乎正在阅读具有不同编码的文本或打印它。

在我的代码中,错误出现在 import 语句中,这让我很困惑。

我在 Visual Studio 2015 上使用 python 64 位 3.3 geotext 是显示错误的库。

请指出在哪里处理这个错误。

【问题讨论】:

  • 使用python3 ?
  • 是的,我正在使用 python 3.3

标签: python python-3.x unicode nlp python-unicode


【解决方案1】:

这是我解决此问题的方法(适用于geotext 0.3.0

检查回溯:

Traceback(最近一次调用最后一次): 文件“pythonTwitterTest.py”,第 5 行,在 从 process.processData 导入 * 文件“C:\OwaisWorkx\Courses\5th Semester\Project\pythonTwitterTest\pythonTwitterTest\process\processData.py”,第 1 行,在 from geotext import GeoText # 用于分类和分隔城市、国家和州/省 文件“c:\Python33\lib\site-packages\geotext__init__.py”,第 7 行,在 从 .geotext 导入 GeoText 文件“c:\Python33\lib\site-packages\geotext\geotext.py”,第 87 行,在 地理文本类(对象): GeoText 中的文件“c:\Python33\lib\site-packages\geotext\geotext.py”,第 103 行 索引 = build_index() 文件“c:\Python33\lib\site-packages\geotext\geotext.py”,第 77 行,在 build_index 城市 = read_table(get_data_path('cities15000.txt'), usecols=[1, 8]) 文件“c:\Python33\lib\site-packages\geotext\geotext.py”,第 54 行,在 read_table 中 对于行中的行: 文件“c:\Python33\lib\site-packages\geotext\geotext.py”,第 51 行,在 lines = (line for line in f if not line.startswith(comment)) 解码中的文件“c:\Python33\lib\encodings\cp1252.py”,第 23 行 返回 codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError:“charmap”编解码器无法解码位置 165 中的字节 0x81:字符映射到

这表明错误实际上是在geotext.py文件中

打开它

geotext.py 并转到第 45 行: 从此改变

with open(filename, 'r') as f

到这里

with open(filename, 'r', encoding='utf-8') as f:

PS:Solution taken from Python-forum.io

【讨论】:

    猜你喜欢
    • 2019-06-01
    • 2012-03-03
    • 2022-09-29
    • 1970-01-01
    • 2018-09-13
    • 2020-04-14
    • 2022-10-25
    相关资源
    最近更新 更多