简体   繁体   中英

How to encode unicode to urlenconing in python 2.7?

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')))
?

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

urllib.quote(u'€'.encode('utf8'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM