python中用于设置/提取字符串值的string.format()是否与之相反

我正在尝试在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'}
  1. 从字符串位置提取数据还不够好,因为我想确保句子的其余部分按照格式
  2. 剥离文本,对文本进行拆分可能会对具有更多位置文本值要提取的较大句子产生意想不到的后果

理想情况下,寻找一个不错的单行代码可以在不使用自定义函数的情况下放入lambda表达式。我目前正在执行2,但是我觉得应该有更好的方法。

yangyong1001 回答:python中用于设置/提取字符串值的string.format()是否与之相反

您正在描述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'}
,

是的,可以使用带有regexnamed 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'}
本文链接:https://www.f2er.com/3155470.html

大家都在问