这里有例子但是有些不明白,请帮我解释一下谢谢(尤其是replcement函数这段)importfileinput,re#Matchesfieldsenclosedinsquarebrackets:field_pat=re.compile(r'\[(.+?)\]')#We'llc...
这里有例子但是有些不明白,请帮我解释一下谢谢(尤其是replcement函数这段)
import fileinput, re
# Matches fields enclosed in square brackets:
field_pat = re.compile(r'\[(.+?)\]')
# We'll collect variables in this:
scope = {}
# This is used in re.sub:
def replacement(match):
code = match.group(1)
try:
# If the field can be evaluated, return it:
return str(eval(code, scope))
except SyntaxError:
# Otherwise, execute the assignment in the same scope...
exec code in scope
# ...and return an empty string:
return ''
lines = []
for line in fileinput.input():
lines.append(line)
text = ''.join(lines)
# Substitute all the occurrences of the field pattern:
print field_pat.sub(replacement, text)
展开