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. import urllib
    urlencode = lambda s: urllib.urlencode({‘x’: s})[2:]
    assert urlencode(“foo bar baz”) == “foo+bar+baz”

  3. 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.