Jay Taylor | programmer notes

Apr/10

25

URLDecode for Python one-liner

Googling for “python urldecode” led me to this, which contained a solution:

import re

def htc(m):
    return chr(int(m.group(1),16))

def urldecode(url):
    rex = re.compile('%([0-9a-hA-H][0-9a-hA-H])',re.M)
    return rex.sub(htc,url)

However, it is seems a overly complex and confusing when it could instead be equivalently  represented like this:

import re
_ud = re.compile('%([0-9a-hA-H]{2})', re.MULTILINE)
urldecode = lambda x: _ud.sub(lambda m: chr(int(m.group(1), 16)), x)

Surely you agree ;)

*Update Dec 6th, 2011*
Okay, so this came up up today when I searched for “python one-liner urldecode” [yea, I forgot that I wrote this! ^^], and I noticed that this isn’t actually a one-liner! Here is a the version with several statements crammed into one line:

import re,sys;print re.sub(r'%([0-9a-hA-H]{2})',lambda m: chr(int(m.group(1),16)), open(sys.argv[1]).read() if len(sys.argv) > 1 else sys.stdin.read())

And here is my .bash_profile alias that I use to make this conveniently accessible from the command line:

alias urldecode="python -c "import re,sys;print re.sub(r'%([0-9a-hA-H]{2})',lambda m: chr(int(m.group(1),16)), open(sys.argv[1]).read() if len(sys.argv) > 1 else sys.stdin.read())""

RSS Feed

No comments yet.

Leave a comment!

You must be logged in to post a comment.

<<

>>

Find it!