反转 str.format() - 通过占位符获取参数
Reversing str.format() - getting arguments by placeholder
逆转 Python 的 str.format 按位置替换参数的过程的最优雅方法是什么?
例如以下
textpattern = 'Please move {0} to {1}'
mystring = 'Please move a to b'
一个deformat
函数:
mystring.deformat(textpattern)
将 return 列表 ['a', 'b']
。变量 textpattern
由用户输入,然后从数据库中动态获取。
我找到的最接近的是a question about scnaf in Python,但略有不同。
您要查找的包 parse
被描述为:parse() 与 format() 相反。
逆转 Python 的 str.format 按位置替换参数的过程的最优雅方法是什么?
例如以下
textpattern = 'Please move {0} to {1}'
mystring = 'Please move a to b'
一个deformat
函数:
mystring.deformat(textpattern)
将 return 列表 ['a', 'b']
。变量 textpattern
由用户输入,然后从数据库中动态获取。
我找到的最接近的是a question about scnaf in Python,但略有不同。
您要查找的包 parse
被描述为:parse() 与 format() 相反。