0

How to encode unicode to urlenconing in python 2.7

I want to encode unicode like '€'.

But I don't konw what should I do...

>>> u='€'
>>> _u=u'€'
>>> u
'\xa2\xe6'
>>> _u
u'\u20ac'
>>> urllib.quote(u)
'%A2%E6'
>>> urllib.quote(_u)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\urllib.py", line 1268, in quote
    return ''.join(map(quoter, s))
KeyError: u'\u20ac'
>>> print urllib.unquote(urllib.quote(u))
€
>>>

Just I need '%A2%E6' Through unicode '€'.

What should I do?

>>> print urllib.unquote(urllib.quote(u'€'.encode('utf8')))
?
chobo
  • 4,830
  • 5
  • 23
  • 36
  • encode to utf-8, then everything goes well. http://stackoverflow.com/questions/5557849/is-there-a-unicode-ready-substitute-i-can-use-for-urllib-quote-and-urllib-unquot – fanlix Feb 21 '13 at 07:34
  • Thank you @fanlix, And I read your link. But I still don't know. That link say using encode(). So I tried that. Please see above. – chobo Feb 21 '13 at 07:45

1 Answers1

0

You should encode unicode string and then use urllib.quote. You yourself wrote the answer to your question

urllib.quote(u'€'.encode('utf8'))
Denis Nikanorov
  • 832
  • 7
  • 16
  • Thank you. Your answer is good. But 'urllib.unquote('%E2%82%AC')' return '%E2%82%AC'. And 'urllib.unquote('%E2%82%AC'')' return '?' – chobo Feb 21 '13 at 08:41
  • At my computer script prints correct result: `€`. May be your problem is "bad" stdout encoding? Try run following code `urllib.unquote(urllib.quote(u'€'.encode('utf8'))).decode('utf8')` – Denis Nikanorov Feb 21 '13 at 12:32