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.