[Tutor] Question on regular expressions
Kent Johnson
kent37 at tds.net
Thu May 25 15:03:57 CEST 2006
Andrew Robert wrote:
> 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?
Use int(s, 16) to convert a base 16 string to an integer, and chr() to
convert the int to a string. So something like this:
lambda s: chr(int(s.group(), 16)))
Kent
More information about the Tutor
mailing list