对于Python中的标准库string template
,是否有函数获取所有标识符的列表?用于获取字符串模板(Python)中所有标识符列表的函数
例如,下面的XML文件:
<Text>Question ${PrimaryKey}:</Text>
<Text>Cheat: ${orientation}</Text>
函数将返回类似PrimaryKey, orientation
对于Python中的标准库string template
,是否有函数获取所有标识符的列表?用于获取字符串模板(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']
这工作!那么它是围绕产生ele [1]列表的短语的方括号吗? –
@云南,它提取第二个元素是占位符名称,如果你'print(list(Formatter()。parse(s)))'你会看到它返回包含不同元素的元组,'[('
所以,你要搜索的所有出现$ {something}在一个xml文件中? – geckon