简体   繁体   English

如何在python 2.7中将unicode编码为urlenconing?

[英]How to encode unicode to urlenconing in python 2.7?

How to encode unicode to urlenconing in python 2.7 如何在python 2.7中将unicode编码为urlenconing

I want to encode unicode like '€'. 我想将Unicode编码为“€”。

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 '€'. 我只需要unicode'€'的'%A2%E6'。

What should I do? 我该怎么办?

>>> print urllib.unquote(urllib.quote(u'€'.encode('utf8')))
?

You should encode unicode string and then use urllib.quote . 您应该encode unicode字符串,然后使用urllib.quote You yourself wrote the answer to your question 您自己写下了问题的答案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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