python charmap 编解码器无法将位置 Y 字符映射中的字节 X 解码为 <undefined>

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

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

UnicodeDecodeError was unhandled by user code Message: 'charmap' codec can't decode byte 0x81 in position 165: character maps to < undefined>

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

在我的代码中,错误出现在导入语句中,这让我很困惑。 enter image description here

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

请指出在哪里可以处理此错误。

最佳答案

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

检查回溯:

Traceback (most recent call last): File "pythonTwitterTest.py", line 5, in from process.processData import * File "C:\OwaisWorkx\Courses\5th Semester\Project\pythonTwitterTest\pythonTwitterTest\process\processData.py", line 1, in from geotext import GeoText # for classifying and seperating City , Country and States/Provinces File "c:\Python33\lib\site-packages\geotext__init__.py", line 7, in from .geotext import GeoText File "c:\Python33\lib\site-packages\geotext\geotext.py", line 87, in class GeoText(object): File "c:\Python33\lib\site-packages\geotext\geotext.py", line 103, in GeoText index = build_index() File "c:\Python33\lib\site-packages\geotext\geotext.py", line 77, in build_index cities = read_table(get_data_path('cities15000.txt'), usecols=[1, 8]) File "c:\Python33\lib\site-packages\geotext\geotext.py", line 54, in read_table for line in lines: File "c:\Python33\lib\site-packages\geotext\geotext.py", line 51, in lines = (line for line in f if not line.startswith(comment)) File "c:\Python33\lib\encodings\cp1252.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 165: character maps to

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

所以打开它

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

with open(filename, 'r') as f

对此

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

附言:Solution taken from Python-forum.io

关于python charmap 编解码器无法将位置 Y 字符映射中的字节 X 解码为 <undefined>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42919076/

相关文章:

python - 绘图时将 pandas (python) 分类 x 轴转换为连续值

python - 使用同一模型创建两个不同的表

python - 在 for 循环期间字典调用变量

python - 按类型有效地汇总项目

python - 为什么 python 中的 0.500000 舍入与使用 '%.0f' 的 45.500000 不同?

php - 替换隐藏在文本中的字符

ios - NSString 替换 unicode 字符

python - 在防火墙后面向 Apache-Spark 提交作业

python - 测试枚举列表中的成员身份失败

python - 为什么 Python 会自动将字符串中的十六进制编码为 UTF-8?