Skip to content

Instantly share code, notes, and snippets.

@Katharine
Created April 6, 2012 18:25
Show Gist options
  • Save Katharine/2321864 to your computer and use it in GitHub Desktop.
Save Katharine/2321864 to your computer and use it in GitHub Desktop.
# Because urllib's doesn't like multibyte unicode.
def escapeurl(url):
safe = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-'
output = ''
for char in url:
if char in safe:
output += char
else:
code = hex(ord(char))[2:]
while len(code) > 0:
if len(code) == 1:
code = '0' + code
output += '%' + code[0:2]
code = code[2:]
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment