2015-02-10 15 views
21

versucht zu entschlüsseln Dekodieren ein url-encoded Zeichenfolge in folgenden WeiseWie ein (doppelt) 'URL-codierte' String in Python

some_string = 'FireShot3%2B%25282%2529.png' 
import urllib 
res = urllib.unquote(some_string).decode() 
res 
u'FireShot3+%282%29.png' 

Original-String FireShot3 (2).png ist. Jede Hilfe wäre willkommen.

Antwort: urllib.unquote_plus(urllib.unquote_plus(some_string)) aufgrund der doppelten Codierung.

+1

Duplikate http://stackoverflow.com/questions/16566069/url-decode-utf- 8-in-Python 100% –

+1

@ MarcusMüller: nicht ganz. Es gibt keine UTF-8-codierten Daten, die Zeichenfolge wurde zweimal ** codiert **. –

+0

In Python 2, 3 oder beides? Die verwendete Bibliothek fn unterscheidet sich von – smci

Antwort

27

Ihre Eingabe ist codiert doppelt. Mit Python 3:

urllib.parse.unquote(urllib.parse.unquote(some_string)) 

Ausgang:

'FireShot3+(2).png' 

jetzt haben Sie die + links.

Edit:

Python 2.7 ist es natürlich:

urllib.unquote(urllib.unquote('FireShot3%2B%25282%2529.png')) 
+0

Sie haben bereits die Funktion 'urllib.unquote' verwendet. –

+5

'unqoute_plus' behandelt das' + 'Zeichen. – user1986059

7

urllib.unquote_plus(urllib.unquote_plus(some_string)) FireShot3 (2).png