1

My program is the follow:

#/usr/bin/python # -*- coding: latin-1 -*- import os, sys from geopy.geocoders import Nominatim geolocator = Nominatim() location = geolocator.reverse("47, 17") cim = location.raw['address']['country'] #cim = "Magyarország" print(cim) f = open('out.txt','w') f.write(cim) f.closed 

The program writes the correct value (Magyarország) at line 12, but at line 14 there is error. ('ascii' codec can't encode character ... in position 10)

And it's interesting, because when I give the value of cim at line 10, both of writings work.

(I'm totally beginner at python)

11
  • cim = u"Magyarország" try pls Commented Aug 17, 2017 at 10:26
  • 1
    Try explicitly encoding as unicode: stackoverflow.com/questions/9942594/… Commented Aug 17, 2017 at 10:28
  • 1
    @TolgahanÜZÜN: where does it say this is Python 2? The error shows they are using Python 3, and they didn't specify an encoding for the open file, so Python uses the system default. Here that default turns out to be ASCII. Commented Aug 17, 2017 at 10:29
  • 1
    @MátéBrunner: so are you using Python 2 instead then? I was assuming Python 3 because you showed cim = "Magyarország" (no u prefix on the string). Commented Aug 17, 2017 at 10:47
  • 1
    @MátéBrunner: then use from io import open to get the Python 3 file I/O system, or encode your data explicitly with f.write(cim.encode('utf8')). Commented Aug 17, 2017 at 10:48

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.