[Tutor] encoding question

eryksun eryksun at gmail.com
Sat Jan 4 21:01:15 CET 2014


On Sat, Jan 4, 2014 at 2:26 PM, Alex Kleider <akleider at sonic.net> wrote:
> The output I get on an Ubuntu 12.4LTS system is as follows:
> alex at x301:~/Python/Parse$ ./IP_info.py3
> Exception raised.
>     IP address is 201.234.178.62:
>         Country: COLOMBIA (CO);  City: b'Bogot\xe1'.
>         Lat/Long: 10.4/-75.2833
>
>
> I would have thought that utf-8 could handle the 'a-acute'.

b'\xe1' is Latin-1. Look in the response headers:

    url = 'http://api.hostip.info/get_html.php?ip=201.234.178.62&position=true'

    >>> response = urllib.request.urlopen(url)
    >>> response.headers.get_charsets()
    ['iso-8859-1']

    >>> encoding = response.headers.get_charsets()[0]
    >>> sp = response.read().decode(encoding).splitlines()
    >>> sp[1]
    'City: Bogotá'


More information about the Tutor mailing list