AP257
AP257

Reputation: 93993

Urllib quoting problem: dealing with â characters from a latin-1 database

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?

Upvotes: 1

Views: 748

Answers (1)

Denis Otkidach
Denis Otkidach

Reputation: 33250

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

Upvotes: 2

Related Questions