[Tutor] Question on regular expressions
Andrew Robert
andrew.arobert at gmail.com
Thu May 25 14:45:54 CEST 2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Great!
Taking this a little further along, I wrote the converted file to a new
file using:
import re,sys
output = open(r'e:\pycode\out_test.txt','wb')
for line in open(r'e:\pycode\sigh.txt','rb') :
output.write( re.sub(r'([^\w\s])', lambda s: '%%%2X' %
ord(s.group()), line))
output.close()
Not elegant but its strictly for test :)
Last part and we can call it a day.
How would you modify the lambda statement to covert a the hex value back
to its original value?
Do I need to incorporate base64.16basedecode somehow?
The original perl code to covert back to normal is:
`perl -ple 's/(?:%([0-9A-F]{2}))/chr hex $1/eg' somefiletxt
Kent Johnson wrote:
> Yes, this is a feature of print, it always inserts a newline. To avoid
> this, use sys.stdout.write() instead of print:
> for line i open(r'e:\pycode\sigh.txt','rb'):
> line = re.sub(r'([^\w\s])', lambda s: '%%%2X' % ord(s.group()), line)
> sys.stdout.write(line)
>
> Kent
>
<snip>
</snip>
- --
Thank you,
Andrew Robert
Systems Architect
Information Technologies
MFS Investment Management
Phone: 617-954-5882
E-mail: arobert at mfs.com
Linux User Number: #201204
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32)
iD8DBQFEdacCDvn/4H0LjDwRAkTWAJ4/KS6WnAgUraPZLmyPCQ45izq5tQCgl7sR
nkZbIauRcdlavA89ZhnDSuM=
=YZPS
-----END PGP SIGNATURE-----
More information about the Tutor
mailing list