URL 'special character' replacements

Brett g Porter bgporter at acm.org
Mon Jan 9 08:56:23 EST 2006


Claude Henchoz wrote:
> Hi guys
> 
> I have a huge list of URLs. These URLs all have ASCII codes for special
> characters, like "%20" for a space or "%21" for an exclamation mark.
> 
> I've already googled quite some time, but I have not been able to find
> any elegant way on how to replace these with their 'real' counterparts
> (" " and "!").
> 
> Of course, I could just replace(), but that seems to be a lot of work.
> 
> Thanks for any help.
> 
> Cheers, Claude
> 

The standard library module 'urllib' gies you two choices, depending on 
the exact behavior you'd like:

http://www.python.org/doc/2.3.2/lib/module-urllib.html
unquote(string)
     Replace "%xx" escapes by their single-character equivalent.

     Example: unquote('/%7Econnolly/') yields '/~connolly/'.

unquote_plus(string)
     Like unquote(), but also replaces plus signs by spaces, as required 
for unquoting HTML form values.


-- 
//  Today's Oblique Strategy (© Brian Eno/Peter Schmidt):
//  Accretion
//  Brett g Porter * BgPorter at acm.org




More information about the Python-list mailing list