How to code accents?

I need to encode external names such as "Misère".

When I do this:

urllib2.quote(name) 

I get an error:

 File "/System/Library/Frameworks/Python.framework/Versions/ 2.5/lib/python2.5/urllib.py", line 1205, in quote res = map(safe_map.__getitem__, s) KeyError: u'\xe8' 

What am I doing wrong?

+6
python escaping urlencode urllib2
source share
2 answers

try urllib2.quote (s.encode ('utf8'))

+12
source share

A slight improvement on @would's answer will include safe characters in the method call. By default, urllib2.quote () only includes / _ - . as a safe character, which means that characters such as : will be converted, making the URL unnecessary.

For example:

 url = https://www.zomato.com/pittsburgh/caffè-damore-catering-pittsburgh print urllib2.quote(url.encode('utf-8')) >>> https%3A//www.zomato.com/pittsburgh/caff%C3%A8-damore-catering-pittsburgh print urllib2.quote(url.encode('utf-8'),':/') >>> https:////www.zomato.com/pittsburgh/caff%C3%A8-damore-catering-pittsburgh 

Note the slight difference in the outputs in the https portion of the URL.

Hope this saves someone else when I need to find out!

0
source share

All Articles