Unix URL for Python

I have a url in this form - http:\\/\\/en.wikipedia.org\\/wiki\\/The_Truman_Show . How can I make it a normal url. I tried using urllib.unquote without much success.

I can always use regular expressions or a simple string to replace stuff. But I believe that there is a better way to handle this ...

+4
source share
3 answers

Have you tried using json.loads from the json module?

 >>> json.loads('"http:\\/\\/en.wikipedia.org\\/wiki\\/The_Truman_Show"') 'http://en.wikipedia.org/wiki/The_Truman_Show' 

The input I'm showing is not exactly what you have. I enclosed it in double quotes to make it valid json.

When you first get it from json, how do you decode it? This is probably a problem.

+5
source

urllib.unquote intended to replace %xx escape codes in the URLs with the characters they represent. It will not be useful for this.

Your "simple string replacement" is probably the best solution.

+11
source

It's too childish - find a library function when you can translate the URL yourself. Since there are no other visible rules, but "/" is replaced by "\ /", you can simply replace it:

 def unescape_this(url): return url.replace(r"\\/", "/") 
+1
source

All Articles