[Tutor] Question on regular expressions

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu May 25 16:01:59 CEST 2006



> for line in open(r'e:\pycode\out_test.txt','rb') :
>    output.write( re.sub(r'([^\w\s])', lambda s: chr(int(s.group(),
> 16))) % ord(s.group()), line))


Let's add some whitespace.

     output.write(re.sub(r'([^\w\s])',
                            lambda s: chr(
                                           int(s.group(), 16)
                                         )
                                      ) % ord(s.group()), line))

I do see at least one too many parens here, so that's something you should 
look at.

But I'd also recommend writing a helper function here.  Just because you 
can do this in one line doesn't mean you have to.  *grin* It might be 
useful to change the lambda back to a helper function.


More information about the Tutor mailing list