简体   繁体   English

如何编码/解码 Python 中的百分比编码 (URL) 字符串?

[英]How do I encode/decode percent-encoded (URL) strings in Python?

How do I decode percent-encoded characters to ordinary unicode characters?如何将百分比编码的字符解码为普通的 unicode 字符?

 "Lech_Kaczy%C5%84ski" ⟶ "Lech_Kaczyński"

For Python 3, using urllib.parse.unquote :对于 Python 3,使用urllib.parse.unquote

 from urllib.parse import unquote print(unquote("Lech_Kaczy%C5%84ski"))

Output: Output:

 Lech_Kaczyński

For Python 2, using urllib.unquote:

import urllib
urllib.unquote("Lech_Kaczy%C5%84ski").decode('utf8')

This will return a unicode string:

u'Lech_Kaczy\u0144ski'

which you can then print and process as usual. For example:

print(urllib.unquote("Lech_Kaczy%C5%84ski").decode('utf8'))

will result in

Lech_Kaczyński

This worked for me:这对我有用:

 import urllib print urllib.unquote('Lech_Kaczy%C5%84ski')

Prints out打印出来

Lech_Kaczyński

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

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