[Tutor] Question on regular expressions

Kent Johnson kent37 at tds.net
Thu May 25 03:16:38 CEST 2006


Andrew Robert wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Wow!!..
> 
> That awesome!
> 
> 
> My goal was not to make it a one-liner per-se..
> 
> I was simply trying to show the functionality I was trying to duplicate.
> 
> Boiling your one-liner down into a multi-line piece of code, I did:
> 
> #!c:\python24\python
> 
> import re,sys
> 
> a = open(r'e:\pycode\csums.txt','rb').readlines()
> 
> for line in a:

You probably want to open the file in text mode, not binary. You don't 
have to read all the lines of the file, you can iterate reading one line 
at a time. Combining these two changes, the above two lines consolidate to
for line in open(r'e:\pycode\csums.txt'):

>     print re.sub(r'([^\w\s])', lambda s: '%%%2X' % ord(s.group()), line)
> 
> 
> 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 :).

It is a way to make an anonymous function, occasionally abused to write 
Python one-liners. You could just as well spell it out:
def hexify(match):
     return ''%%%2X' % ord(match.group())

print re.sub(r'([^\w\s])', hexify, line)

> 
> The un-named function does in-place transformation of the character to
> the established hex value.
> 
> 
> Does this sound right?
Yes.

Kent



More information about the Tutor mailing list