我正在尝试在python中解析字符串以提取值。我们知道字符串可以像这样格式化变量:
food = {'fruit':'apple', 'vegetable':'carrot'}
sentence = "I ate a {fruit} and a {vegetable} today".format(**food)
有相反的方法吗?从已知文本模板中提取值?像这样:
food = sentence.extract("I ate a {fruit} and a {vegetable} today")
# food gets set as dictionary {'fruit':'apple', 'vegetable':'carrot'}
理想情况下,寻找一个不错的单行代码可以在不使用自定义函数的情况下放入lambda表达式。我目前正在执行2,但是我觉得应该有更好的方法。
答案 0 :(得分:4)
您正在描述parse
的基本用法:
>>> from parse import parse
>>> sentence = 'I ate a apple and a carrot today'
>>> template = 'I ate a {fruit} and a {vegetable} today'
>>> parse(template, sentence).named
{'fruit': 'apple', 'vegetable': 'carrot'}
答案 1 :(得分:2)
是的,可以使用带有regex的named groups:
import re
sentence = "I ate a apple and a carrot today"
matches = re.match(r"I ate a (?P<fruit>.*?) and a (?P<vegetable>.*?) today", sentence)
print(matches.groupdict())
将打印
{'fruit': 'apple', 'vegetable': 'carrot'}