如何将unicode编码为urlenconing中的python 2.7如何将unicode编码为python 2.7中的urlenconing?
我想编码像'€'的Unicode。
但我却不知道该怎么办......
>>> 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))
€
>>>
只是我需要 '%A2%E6' 通过unicode的 '€'。
我该怎么办?
>>> print urllib.unquote(urllib.quote(u'€'.encode('utf8')))
?
编码为utf-8,然后一切顺利。 http://stackoverflow.com/questions/5557849/is-there-a-unicode-ready-substitute-i-can-use-for-urllib-quote-and-urllib-unquot – fanlix 2013-02-21 07:34:15
谢谢@fanlix,我读你的链接。但我仍然不知道。该链接说使用encode()。所以我试了一下。请参阅上文。 – chobo 2013-02-21 07:45:52