python-3.x - 用于提取文本中位置的 geograpy3 库,给出 UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 276
问题描述
我正在尝试使用 python 中的 geography3 库从文本中提取位置。
import geograpy
address = 'Jersey City New Jersey 07306'
places = geograpy.get_place_context(text = address)
我得到以下错误UnicodeDecodeError:
~\Anaconda\lib\site-packages\geograpy\places.py in populate_db(self)
28 with open(cur_dir + "/data/GeoLite2-City-Locations.csv") as info:
29 reader = csv.reader(info)
---> 30 for row in reader:
31 print(row)
32 cur.execute("INSERT INTO cities VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", row)
~\Anaconda\lib\encodings\cp1252.py in decode(self, input, final)
21 class IncrementalDecoder(codecs.IncrementalDecoder):
22 def decode(self, input, final=False):
---> 23 return
codecs.charmap_decode(input,self.errors,decoding_table)[0]
24
25 class StreamWriter(Codec,codecs.StreamWriter):
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 276: character maps to <undefined>
经过一番调查,我尝试修改places.py文件并在该行添加encoding =“utf-8” -----> 30
with open(cur_dir + "/data/GeoLite2-City-Locations.csv", encoding="utf-8") as info:
但它仍然给我同样的错误。我还尝试将 GeoLite2-City-Locations.csv 保存在我的桌面上,然后尝试使用相同的代码读取它。
with open("GeoLite2-City-Locations.csv", encoding="utf-8") as info:
reader = csv.reader(info)
for row in reader:
print(row)
它工作得很好,并打印了 GeoLite2-City-Locations.csv 的所有行。我无法理解这个问题!
解决方案
作为 geograpy3 的提交者来重现您的问题,我向最新的 geograpy3 https://github.com/somnathrakshit/geograpy3/blob/master/tests/test_extractor.py添加了一个测试:
结果:
['Jersey', 'City'
所以你可以简单地切换到最新版本。
def testStackoverflow54077973(self):
'''
see https://stackoverflow.com/questions/54077973/geograpy3-library-for-extracting-the-locations-in-the-text-gives-unicodedecodee
'''
address = 'Jersey City New Jersey 07306'
e=Extractor(text=address)
e.find_entities()
self.check(e.places,['Jersey','City'])
推荐阅读
- reactjs - Jest 不能在模块外使用 import 语句
- enums - rust:如果枚举具有从泛型结构派生的所有变体,那么检查存储在枚举中的泛型的最佳方法是什么?
- javascript - 如何在 Vue 中保持 props 数据持久化?
- sql-server - T-SQL 游标缺少第一个值并分批执行
- c++ - 指针与 nullptr_t 的模板参数类型推导
- css - 如何在外部元素上设置属性
- django - Django 表单未在 POST 中提交
- java - Jboss 7 CICS 连接错误 InteractionSpec 不是 ECIInteractionSpec 类型
- events - 在 Microsoft Graph API 上使用 lastmodifiedDateTime 过滤日历或事件资源
- javascript - 在 Discord.JS 中创建 dog 命令