I've got the following little Python 2.7 script:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import geoip2.database
def ret_country_iso(ip):
reader = geoip2.database.Reader('/usr/local/geoip/GeoLite2-Country.mmdb')
response = reader.country(ip)
return response.country.iso_code.lower()
result = ret_country_iso("8.8.8.8")
print result
result += "Роман"
print result
where, as you can see, I first figure out the country where the "8.8.8.8" IP is located (this returns "us" - see below) and then I concatenate a short string to it which contains some Russian characters.
Result:
# ./script.py
us
Traceback (most recent call last):
File "./script.py", line 12, in <module>
result += "Роман"
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)
Now, if I try the following instead
#!/usr/bin/python
# -*- coding: utf-8 -*-
result = "us"
print result
result += "Роман"
print result
Then everything's ok:
./script.py
us
usРоман
Obviously then, the 'ret_country_iso()' function returns something different than the literal "us" string, my Python is too poor though to say.
How to correct the above?
EDIT: following the advice of snakecharmerb, the following works:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import geoip2.database
def ret_country_iso(ip):
reader = geoip2.database.Reader('/usr/local/geoip/GeoLite2-Country.mmdb')
response = reader.country(ip)
return response.country.iso_code.lower().encode('utf-8')
result = ret_country_iso("8.8.8.8")
print result
result += "Роман"
print result
resultmight be aunicodeobject; doesresult += u"Роман"work?result +=line or the secondprint resultline?str_result = result.encode('utf-8')(you can use other encodings, but they must be able to handle cyrillic characters)