Escaping a URL within XML using Python



I have a Python routine which writes an XML document and one of the tags contains a URL. I'm not allowed to use ampersands unescaped within the tags, so I call urllib.quote(). The problem is that this escapes everything to the extent that the URL is unusable or unrecognisable. I've tried to use the safe argument, but can't be sure I'll catch everything it wants to mangle.



urllib.quote(my_url, safe='%:=/?~#+!$,;\'@()*'))


There isn't an inverse argument (dangerous=?) giving the characters I do want escaped (possibly just ?). What to do?


No comments:

Post a Comment