[Tutor] Question on regular expressions

Alan Gauld alan.gauld at freenet.co.uk
Thu May 25 06:17:23 CEST 2006


> a = open(r'e:\pycode\csums.txt','rb').readlines()
>
> for line in a:
>    print re.sub(r'([^\w\s])', lambda s: '%%%2X' % ord(s.group()), 
> line)

Or just

for line in open(r'e:\pycode\csums.txt','rb'):
   print.....

> Breaking down the command, you appear to be calling an un-named 
> function
> to act against any characters trapped by the regular expression.
>
> Not familiar with lamda :).

You ae absolutely right.
It creates an un-named(or anonymous function). :-)

> The un-named function does in-place transformation of the character 
> to
> the established hex value.

Its actually the call to re.sub() that makes in in place.

> How would you reverse the process from a python point of view?

Just write a reverse function for the lamda...

Alan G. 




More information about the Tutor mailing list