我一直在摆弄 python geopy,并尝试了文档中给出的基本命令。但我得到了 UnicodeEncodeError 尝试 raw
时命令(将查询地理定位到地址和坐标)
print(location.raw)
错误
UnicodeEncodeError: 'charmap' codec can't encode character '\xa9' in position 83: character maps to <undefined>
然后我尝试了另一种方式(找到一组坐标对应的地址)
print(location.address)
我遇到了同样的错误
UnicodeEncodeError: 'charmap' codec can't encode character '\u0101' in position 10: character maps to <undefined>
我试过
print((location.address).encode("utf-8"))
,现在没有收到任何错误,但打印的输出是这样的 b'NH39, Mirz\xc4\x81pur
当使用
print((location.raw).encode("utf-8"))
我收到错误AttributeError: 'dict' object has no attribute 'encode'
谁能告诉我这里发生了什么以及我应该怎么做才能获得正确的输出?
编辑: (被标记为重复后)
Based on the solution given in this problem I am reporting on how it does not solve my problem
我想知道的是为什么我在尝试文档中给出的基本示例编码时会得到 UnicodeEncodeError 并且它确实回答了这个问题。
如果我想将它用作应用程序,我该如何解决错误,并且我不能让应用程序在单独的 IDE 上运行或将输出发送到外部文件,因为我的应用程序将根据 geopy 的输出运行,我希望应用程序运行在终端中,就像我的其他应用程序一样。
最佳答案
问题是您的控制台未设置为正确显示您尝试打印的 Unicode 字符。
在 Windows 7 中,对于 Python 3.3 + ,您可以将 python 控制台的代码页更改为 65001 以显示 unicode 字符。在终端运行以下 -
chcp 65001
在开始 python/你的脚本之前。
关于Python UnicodeEncodeError : 'charmap' codec can't encode when using GEOPY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31870452/