2

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'
Philip
  • 944
  • 11
  • 26

1 Answers1

3

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.)

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
  • Could you elaborate a bit. I have been trying to decode and then encode the characters. But I Im still a novice. If you could direct me to some example it would help me a lot. Thank you for your time and for the direction. – Philip Apr 13 '14 at 18:27
  • 1
    @PhilipHoyos: This is a good explanation of Unicode in Python 2: http://www.diveintopython.net/xml_processing/unicode.html – RichieHindle Apr 13 '14 at 18:29