简体   繁体   中英

How decode url to windows-1251 in python

How decode url to windows-1251 in python 2.7 and python 3.2?
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)

Never ever use "international" strings without the 'u' prefix. Without it, your "string" is just a chunk of bytes and python has no idea what to do with it. 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

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