regex/lambda black magic

Andrew Robert andrew.arobert at gmail.com
Thu May 25 18:42:30 CEST 2006


Max Erickson wrote:
<snip>

</snip>

> Try getting rid of the lamba, it might make things clearer and it 
> simplifies debugging. Something like(this is just a sketch):
> 
> 
> max
> 
Yeah.. trying to keep everything on one line is becoming something of a
problem.

To make this easier, I followed something from another poster and came
up with this.

import re,base64

# Evaluate captured character as hex
def ret_hex(value):
	return base64.b16encode(value)

def ret_ascii(value):
	return base64.b16decode(value)

# Evaluate the value of whatever was matched
def eval_match(match):
 	return ret_ascii(match.group(0))

# Evaluate the value of whatever was matched
# def eval_match(match):
# 	return ret_hex(match.group(0))

out=open(r'e:\pycode\sigh.new2','wb')

# Read each line, pass any matches on line to function for
# line in file.readlines():
for line in open(r'e:\pycode\sigh.new','rb'):
	print (re.sub('[^\w\s]',eval_match, line))



The char to hex pass works but omits the leading % at the start of each
hex value.

ie. 22 instead of %22


The hex to char pass does not appear to work at all.

No error is generated. It just appears to be ignored.



More information about the Python-list mailing list