-1

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 '\u0101' 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.

Community
  • 1
  • 1
Tony Roczz
  • 2,366
  • 6
  • 32
  • 59

2 Answers2

1

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.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • If i would like to use it in an application then how can i do it? – Tony Roczz Aug 07 '15 at 06:37
  • if you do `chcp 65001` once, you should be good to go for any number of times the application is run in that particular command prompt. Otherwise are you asking for a permanent solution to change the code page? – Anand S Kumar Aug 07 '15 at 06:39
  • Yes something like that. Is there a way to automatically do this before that particular script is run? – Tony Roczz Aug 07 '15 at 06:41
  • You can try the option given here - http://stackoverflow.com/questions/7432545/change-codepage-in-cmd-permanently - for that. – Anand S Kumar Aug 07 '15 at 06:43
  • 1
    `chcp 65001` isn't generally a good idea. There are serious bugs in the Windows Console that can result in double-printing or hangs using this code page for all applications using the C standard library IO routines (including Python). Consider: [win_unicode_console](https://pypi.python.org/pypi/win_unicode_console) – bobince Aug 07 '15 at 16:08
0
print json.dumps(address_object.raw,ensure_ascii=False, encoding="utf-8").encode('utf-8')

please check, hope this works for you

Amir reza Riahi
  • 1,540
  • 2
  • 8
  • 34