2011-09-23 59 views
-2

如何unicode字符串這樣解碼:Python的解碼文本ASCII

什麼%2527s%2bthe%2btime%252C%2bnow%253F

成ASCII碼是這樣的:

什麼是+時間+現在

+0

http://stackoverflow.com/questions/275174時/ how-do-i-perform-html-decoding-encoding-using-python-django – dm03514

+0

你開始的字符串不在unicode中。 –

+0

「ascii」vs「unicode」與您所遇到的問題完全不同。實際上,它幾乎沒有更多的不同。 –

回答

6

,該字符串被解碼兩次,所以我們需要所享有兩次把它找回來

In [1]: import urllib 
In [2]: urllib.unquote(urllib.unquote("what%2527s%2bthe%2btime%252c%2bnow%253f")) 
Out[3]: "what's+the+time,+now?" 
+0

至少外層的'unquote'可能想成爲'unquote_plus';我猜這些'+'最初是空格,以HTML格式提交(與常規URL編碼相比,''具有略微不同的'+'處理)。但是,是的,雙重編碼的字符串是「某人在這裏做錯了...」的紅旗...... – bobince

0

是這樣的嗎?

title = u"what%2527s%2bthe%2btime%252c%2bnow%253f" 
print title.encode('ascii','ignore') 

另外,看看你的情況this

0

你可以轉換%(十六進制)轉義字符如下:

import re 

def my_decode(s): 
    re.sub('%([0-9a-fA-F]{2,4})', lambda x: unichr(int(x.group(1), 16)), s) 

s = u'what%2527s%2bthe%2btime%252c%2bnow%253f' 
print my_decode(s) 

結果在unicode字符串

u'what\u2527s+the+time\u252c+now\u253f' 

不知道你怎麼知道\ u2527轉換爲一個單引號,或跌落\ u253f和\ u252c字符轉換爲ASCII

+0

Doh,我認爲Kent有權利 – barryp

相關問題