views:

22

answers:

1

I need to get an â character into a format that can be passed to a URL. I'm obtaining some names as a json list, and then passing them elsewhere.

result = json.load(urllib2.urlopen(LIST_URL), encoding='latin-1')
for item in result:
    name = item["name"]
    print name
    print urllib2.quote(name.lower())

This produces a urllib error when the name is Siân:

Siân
Line 24 - print urllib2.quote(mp_name.lower())
/usr/lib/python2.6/urllib.py -- quote((s=u'si\xe2n', safe='/'))
KeyError(u'\xe2')

Please could anyone advise?

+1  A: 

quote() function requires str argument, not unicode. Use urllib2.quote(name.lower().encode('latin1')) (assuming your site accepts latin1 encoding).

Denis Otkidach
Thanks! But why does item["name"] come out as unicode? Don't I specify encoding='latin-1' in my json query? Please help me understand...
AP257