Skip to main content
 首页 » 编程设计

python之有没有办法在猎豹模板中获取所有占位符的列表

2024年10月24日8qq号

例如,如果我有一个模板,例如

"SELECT * FROM myOrders WHERE order_id = ${orderId} and order_date = ${orderDate}" 

我想获得该模板中所有占位符的列表,即 ['orderId', 'orderDate']

请您参考如下方法:

我可以使用正则表达式来提取这些占位符。

附注尽管如此,在 Cheetah API 中(也许在 Template 类中)拥有该功能还是不错的。

例子:

import re 
templateDef = """ 
SELECT * FROM myOrders WHERE order_id = ${orderId} and order_date = ${orderDate} 
""" 
placeholders = re.findall(r"\${(\w+)}", templateDef) 
print placeholders