Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform URL string into normal string in Python (%20 to space etc)

Tags:

python

string

Is there any way in Python to transform this %CE%B1%CE%BB%20 into this αλ which is its real representation?

like image 974
hytromo Avatar asked Aug 01 '12 21:08

hytromo


People also ask

How do you get rid of %20 in python?

replace('%20+', '') will replace '%20+' with empty string. Isn't just '%20' you need to replace? There's a lot more than %20 that you need to deal with.

How does Python handle space in URL?

The proper way of encoding a space in the query string of a URL is the + sign. See Wikipedia and the HTML specification. As such urllib. quote_plus() should be used instead when encoding just one key or value, or use urllib.

How do I remove a URL from a string in Python?

Use the re. sub() method to remove URLs from text, e.g. result = re. sub(r'http\S+', '', my_string) .


1 Answers

For python 2:

>>> import urllib2 >>> print urllib2.unquote("%CE%B1%CE%BB%20") αλ  

For python 3:

>>> from urllib.parse import unquote >>> print(unquote("%CE%B1%CE%BB%20")) αλ 

And here's code that works in all versions:

try:     from urllib import unquote except ImportError:     from urllib.parse import unquote  print(unquote("%CE%B1%CE%BB%20")) 
like image 147
Igor Chubin Avatar answered Sep 28 '22 11:09

Igor Chubin