我正在尝试使用 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 的所有行。我无法理解这个问题!