[Tutor] Question on regular expressions

Kent Johnson kent37 at tds.net
Thu May 25 15:56:02 CEST 2006


Andrew Robert wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Hi all,
> 
> I tried:
> 
> 
> output = open(r'e:\pycode\new_test.txt','wb')
> 
> 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))
> 
> 
> This generated the traceback:
> 
> File "E:\pycode\sample_decode_file_specials_from_hex.py", line 8
>     output.write( re.sub(r'([^\w\s])', lambda s: chr(int(s.group(),
> 16))) % ord(s.group()), line))
> 
>                          ^
> SyntaxError: invalid syntax
> 
> 
> By any chance, do you see where the syntax issue is?

Take out " % ord(s.group())" - the result of chr() is the actual string 
you want, not a format string.

The syntax error is caused by mismatched parentheses.

Kent



More information about the Tutor mailing list