Search code examples
windowsunicodepython-3.4geopy

Python 3 - GeoPy and encoding


I'm using DictWriter to write a dictionary to a csv after some geolocation work.

location = geolocator.reverse(coords)
row["address"] = location.address
writer.writerow(row)

Which generates this:

File "C:\bin64\python\3.4.3\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u200e' in
    position 118: character maps to <undefined>

Solution

  • My problem was in how I was opening the file. I suppose I should have posted that in the question. I needed to set the encoding upon opening the file.

    with open('results.csv', mode='w', encoding='utf-8', newline='') as file:
        ...