【发布时间】:2017-03-01 16:00:16
【问题描述】:
我熟悉使用Templates 将变量插入字符串的能力,如下所示:
Template('value is between $min and $max').substitute(min=5, max=10)
我现在想知道的是是否可以反过来做。我想获取一个字符串,并使用模板从中提取值,这样我就有了一些包含提取值的数据结构(最好只是命名变量,但字典很好)。例如:
>>> string = 'value is between 5 and 10'
>>> d = Backwards_template('value is between $min and $max').extract(string)
>>> print d
{'min': '5', 'max':'10'}
这可能吗?
【问题讨论】: