regular expressions use

Peter Otten __peter__ at web.de
Mon Aug 22 06:20:06 EDT 2005


max(01)* wrote:

>  would like to do some uri-decoding, which means to translate patterns
> like "%2b/dhg-%3b %7E" into "+/dhg-; ~": in practice, if a sequence like
> "%2b" is found, it should be translated into one character whose hex
> ascii code is 2b.
> 
> i did this:
> 
> ...
> import re
> import sys
> 
> modello = re.compile("%([0-9a-f][0-9a-f])", re.IGNORECASE)
> 
> def funzione(corrispondenza):
>     return chr(eval('0x' + corrispondenza.group(1)))

You can specify the base for str to int conversion, e. g:

return chr(int(corrispondenza.group(1), 16))

And then there is also urllib.unquote() in the library.

Peter




More information about the Python-list mailing list