[Tutor] Question on regular expressions
Kent Johnson
kent37 at tds.net
Thu May 25 16:38:39 CEST 2006
Andrew Robert wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> When I alter the code to:
>
> import re,sys
>
> 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)))
> , line)
>
> output.close()
>
> I get the trace:
>
> Traceback (most recent call last):
> File "E:\pycode\sample_decode_file_specials_from_hex.py", line 8, in ?
> output.write( re.sub(r'([^\w\s])', lambda s: chr(int(s.group(),
> 16))) , line)
> TypeError: sub() takes at least 3 arguments (2 given)
>
> It appears that the code is not recognizing the line.
>
> I checked the parentheses and they appear to be properly enclosed.
>
> Any ideas?
You have an argument in the wrong place. Stop trying to do everything in
one line! Put the lambda in a def'd function. Put the re.sub on it's own
line. You are tripping over unnecessary complexity. I'm not going to fix
it any more.
Kent
More information about the Tutor
mailing list