简体   繁体   中英

Python UnicodeEncodeError: 'charmap' codec can't encode when using GEOPY

I have been fiddling with python geopy and I tried the basic commands given in the documentation. But I am getting the UnicodeEncodeError when trying the raw command(to geolocate a query to an address and coordinates)

print(location.raw)

Error UnicodeEncodeError: 'charmap' codec can't encode character '\\xa9' in position 83: character maps to <undefined>

Then I tried the other way around (To find the address corresponding to a set of coordinates)

print(location.address)    

I am getting the same error UnicodeEncodeError: 'charmap' codec can't encode character '\ā' in position 10: character maps to <undefined>

I tried print((location.address).encode("utf-8")) , now am not getting any error but the output printed is like this b'NH39, Mirz\\xc4\\x81pur

and when using print((location.raw).encode("utf-8")) I am getting error

AttributeError: 'dict' object has no attribute 'encode'

Can anyone tell me what is going on here and what I should do to get a proper output?

Edit: (After being marked as duplicate)

Based on the solution given in this problem I am reporting on how it does not solve my problem

What I wanted to know is why do I get the UnicodeEncodeError when trying out the basic sample codings given in the documentation and it did answer for that.

If I want to use it an application how do I solve the error and I cannot have the application running on separate IDE or send the output to a external file since my application will function based on the output from geopy, I want the application to run in the terminal as my other applications do.

The issue is that your console is not setup to correctly show the unicode characters that you are trying to print.

In Windows 7, for Python 3.3 + , you can change the code page of python console to 65001 for it to show unicode characters. In terminal run the following -

chcp 65001

Before starting python/your script.

print json.dumps(address_object.raw,ensure_ascii=False, encoding="utf-8").encode('utf-8')

请检查,希望这对你有用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM