我從HTTP頭獲得了一個字符串,但它已被轉義..我可以使用什麼函數來隱藏它?來自HTTP的Unescape Python字符串
myemail%40gmail.com -> [email protected]
請問urllib.unquote()是否要走?
我從HTTP頭獲得了一個字符串,但它已被轉義..我可以使用什麼函數來隱藏它?來自HTTP的Unescape Python字符串
myemail%40gmail.com -> [email protected]
請問urllib.unquote()是否要走?
我很確定urllib的unquote
是這樣做的常用方法。
>>> import urllib
>>> urllib.unquote("myemail%40gmail.com")
'[email protected]'
還有unquote_plus
:
像所享有(),也用空格代替加號,按要求unquoting HTML表單值。
是的,看起來urllib.unquote()
完成該任務。 (I tested it against your example on codepad。)
在Python 3中,這些函數是urllib.parse.unquote
和urllib.parse.unquote_plus
。
後者例如用於在HTTP的URL的查詢字符串,其中,所述空格字符()傳統上編碼爲加號(
+
),並且+
是百分比編碼到%2B
。
除了這些,還有unquote_to_bytes
將給定的編碼字符串轉換爲bytes
,當編碼未知或編碼數據是二進制數據時可以使用它。但是沒有unquote_plus_to_bytes
,如果你需要它,你可以這樣做:是否使用unquote
或unquote_plus
可在URL encoding the space character: + or %20
def unquote_plus_to_bytes(s):
if isinstance(s, bytes):
s = s.replace(b'+', b' ')
else:
s = s.replace('+', ' ')
return unquote_to_bytes(s)
更多信息。
K,只是想確定..我討厭使用看起來可以完成這個工作的函數,但最終只能用我做的幾個例子來工作,並用真實世界的變量來打破。嘿嘿。 然後就不可能找出問題..:P – Ian 2009-04-23 04:59:45