2009-04-23 62 views

回答

34

我很確定urllib的unquote是這樣做的常用方法。

>>> import urllib 
>>> urllib.unquote("myemail%40gmail.com") 
'[email protected]' 

還有unquote_plus

像所享有(),也用空格代替加號,按要求unquoting HTML表單值。

+1

K,只是想確定..我討厭使用看起來可以完成這個工作的函數,但最終只能用我做的幾個例子來工作,並用真實世界的變量來打破。嘿嘿。 然後就不可能找出問題..:P – Ian 2009-04-23 04:59:45

1

在Python 3中,這些函數是urllib.parse.unquoteurllib.parse.unquote_plus

後者例如用於在HTTP的URL的查詢字符串,其中,所述空格字符()傳統上編碼爲加號(+),並且+是百分比編碼到%2B

除了這些,還有unquote_to_bytes將給定的編碼字符串轉換爲bytes,當編碼未知或編碼數據是二進制數據時可以使用它。但是沒有unquote_plus_to_bytes,如果你需要它,你可以這樣做:是否使用unquoteunquote_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) 

更多信息。