Question

Im trying to retrieve coordinates from an API but one of the json objects contains danish letters, and I keep getting an error. This is what I have:

# -*- coding: utf-8 -*-
import urllib2
import json
import codecs

url='http://geo.oiorest.dk/adresser.json?postnr=1577&vejnavn=bernstorffsgade&husnr=16'

addressline = "%s, %s"

try:
    data = urllib2.urlopen(url).read().decode('utf-8')
    adresser = json.loads(data, encoding='utf-8')

    for adresse in adresser:
        print addressline % \
            (adresse['etrs89koordinat']['øst'],
             adresse['etrs89koordinat']['nord'])

except urllib2.HTTPError, e:
    print "HTTP error: %d" % e.code
except urllib2.URLError, e:
    print "Network error: %s" % e.reason.args[1]

The Error I get:

KeyError: '\xc3\xb8st'
Was it helpful?

Solution

Your decoded data contains Unicode strings, so you need to look things up using Unicode strings:

print addressline % \
    (adresse[u'etrs89koordinat'][u'øst'],
     adresse[u'etrs89koordinat'][u'nord'])

(You might find it works for strings that only contain unaccented characters whether you use Unicode strings or not, because of the automatic conversion between Unicode and your default encoding, but that won't work for accented characters.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top