简体   繁体   English

如何在python中将URL解码为Windows-1251

[英]How decode url to windows-1251 in python

How decode url to windows-1251 in python 2.7 and python 3.2? 如何在python 2.7和python 3.2中将URL解码为Windows-1251?
Example: 例:

a = "пример"
urllib.quote_plus(a)
'%D0%BF%D1%80%D0%B8%D0%BC%D0%B5%D1%80' (unicode)

How to make It in windows-1251 (%EF%F0%E8%EC%E5%F0) 如何在Windows-1251中制作(%EF%F0%E8%EC%E5%F0)

Never ever use "international" strings without the 'u' prefix. 永远不要使用没有'u'前缀的“国际”字符串。 Without it, your "string" is just a chunk of bytes and python has no idea what to do with it. 没有它,您的“字符串”只是一小部分字节,而python不知道如何处理它。 With the prefix, everything's easy: 使用前缀,一切都很容易:

a = u"пример" 

print urllib.quote_plus(a.encode('utf8'))
## %D0%BF%D1%80%D0%B8%D0%BC%D0%B5%D1%80

print urllib.quote_plus(a.encode('cp1251'))
## %EF%F0%E8%EC%E5%F0

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

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