Python Url Encoding

I looked in the Python Library Reference for a function to encode special characters in strings to be able to be put in Urls (and Uri’s), but found none.

Maybe I missed it?

Anyways, here is an implementation I wrote based on this article:

HexCharacters = "0123456789abcdef"

def UrlEncode(s):
    r = ''
    for c in s:
        o = ord(c)
        if (o >= 48 and o <= 57) or \
            (o >= 97 and o <= 122) or \
            (o >= 65 and o <= 90) or \
            o == 36 or o == 45 or o == 95 or \
            o == 46 or o == 43 or o == 33 or \
            o == 42 or o == 39 or o == 40 or \
            o == 41 or o == 44:
            r += c
        else:
            r += '%' + CleanCharHex(c)
    return r

def CleanCharHex(c):
    o = ord(c)
    r = HexCharacters[o / 16]
    r += HexCharacters[o % 16]
    return r

note: I used the character numbers instead the characters to compare with so I could do greater than and lesser than for the alphanumeric numbers. Maybe testing with a bitmask would be more efficient.

I have to write almost everytime I work with python my own something to hex function which doesn’t add the ‘0x’ in front the built-in hex does.

Either I can’t search or Python hasn’t got enough batteries included. I guess the first, if not I`ll submit my batteries.

6 thoughts on “Python Url Encoding”

  1. Your UrlEncode function can be refactored:

    def UrlEncode(s):
    r = ”
    for c in s:
    if c in HexCharacters:
    o = ord(c)
    r += ‘%’ + CleanCharHex(c)
    else:
    r += c
    return r

  2. urlib mothed as follows:

    st = st.encode(โ€™gb2312?)
    m = {โ€™parโ€™:st,}
    s = urllib.urlencode(m)
    print s

  3. import urllib
    urlencode = lambda s: urllib.urlencode({‘x’: s})[2:]
    assert urlencode(“foo bar baz”) == “foo+bar+baz”

  4. Thanks for this code snippet. On my device a don’t have urllib, thus your approach saved my day ๐Ÿ™‚

Leave a Reply

Your email address will not be published. Required fields are marked *