获取字符串模板中所有标识符的列表的函数(Python) [英] Function to get list of all identifiers in String Template (Python)

查看:99
本文介绍了获取字符串模板中所有标识符的列表的函数(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Python中的标准库string template,是否有获取所有标识符列表的函数?

For standard library string template in Python, is there a function to get a list of all identifiers?

例如,使用以下xml文件:

For example, with the following xml file:

<Text>Question ${PrimaryKey}:</Text>
<Text>Cheat: ${orientation}</Text>

该函数将返回类似PrimaryKey, orientation

推荐答案

您可以使用 string.Formatter.parse

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']

这篇关于获取字符串模板中所有标识符的列表的函数(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆