listing10-11.py

来自「《Beginning Python--From Novice to Profes」· Python 代码 · 共 32 行

PY
32
字号
# templates.pyimport 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 ''# Get all the text as a single string:# (There are other ways of doing this; see Chapter 11)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)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?