Search code examples
pythonfile-ioutf-8python-unicodegoogle-maps-static-api

UnicodeEncodeError while redirecting output to file - python 2.7


I have to redirect the output of a googlemaps API function to a file. I have a few objects in my pandas dataframe and I am getting their addresses through the google API.

import googlemaps
from __future__ import print_function
f=open('output.csv', 'w')
for idx, row in df.iterrows():
    gmaps = googlemaps.Client(key="my_key")
    reverse_result = gmaps.reverse_geocode((row['lat'], row['lon']), result_type='administrative_area_level_3')
    for result in reverse_result:
        print (row['object'],result["formatted_address"], file=f)

As I do so, I get the error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 15: ordinal not in range(128)

If I simply print it to the screen it works flawlessly and it displays a well formatted address; the issue is in the process of writing it to an external file. I think I understand what the error message is telling me - there is some character in the output which is not encoded in utf8 - but I don't know how to work around it and have my output written to my csv.


Solution

  • Problem solved. Turns out the issue wasn't the result from the API but the row['object'] from the df. I wrote a simple function

    def force_to_unicode(text):
        return text if isinstance(text, unicode) else text.decode('utf8')
    

    and then I just edited the second for loop:

    for result in reverse_result:
            a=force_to_unicode(row['object']) 
            b=result['formatted_address']
            print(a,',',b, file=f) #write result to csv file