在Python的标准库字符串模板
中,是否有一个函数可以获取所有标识符的列表?
比如,以下是一个xml文件:
<Text>Question ${PrimaryKey}:</Text>
<Text>Cheat: ${orientation}</Text>
该函数将返回类似于
PrimaryKey,orientation
的内容。在Python的标准库字符串模板
中,是否有一个函数可以获取所有标识符的列表?
比如,以下是一个xml文件:
<Text>Question ${PrimaryKey}:</Text>
<Text>Cheat: ${orientation}</Text>
PrimaryKey,orientation
的内容。from string import Formatter
s="""<Text>Question ${PrimaryKey}:</Text>
<Text>Cheat: ${orientation}</Text>"""
print([ele[1] for ele in Formatter().parse(s) if ele[1]])
['PrimaryKey', 'orientation']
print(list(Formatter().parse(s)))
,您将看到它返回不同元素的元组,其中包含 [('<Text>Question $', 'PrimaryKey', '', None), (':</Text>\n<Text>Cheat: $', 'orientation', '', None), ('</Text>', None, None, None)]
。 - Padraic Cunningham一个叫做 get_identifiers() 的方法应该会有所帮助
from string import Template
s="""<Text>Question ${PrimaryKey}:</Text>
<Text>Cheat: ${orientation}</Text>"""
temp = Template(s)
print(temp.get_identifiers())
返回
['PrimaryKey', 'orientation']