yak
yak

Reputation: 3930

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

How do I decode percent-encoded characters to ordinary unicode characters?

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

Upvotes: 22

Views: 14099

Answers (3)

Mateen Ulhaq
Mateen Ulhaq

Reputation: 27201

For Python 3, using urllib.parse.unquote:

from urllib.parse import unquote

print(unquote("Lech_Kaczy%C5%84ski"))

Output:

Lech_Kaczyński

Upvotes: 32

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

Upvotes: 15

answerzilla
answerzilla

Reputation: 201

This worked for me:

import urllib

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

Prints out

Lech_Kaczyński

Upvotes: 1

Related Questions