【问题标题】:Reversing str.format() - getting arguments by placeholder反转 str.format() - 通过占位符获取参数
【发布时间】:2016-03-20 12:47:40
【问题描述】:

在按位置替换参数时,逆转 Python 的 str.format 过程的最优雅的方法是什么?

例如以下

textpattern = 'Please move {0} to {1}'
mystring = 'Please move a to b'

deformat 函数:

mystring.deformat(textpattern)

将返回列表['a', 'b']。变量textpattern 正在由用户输入,然后从数据库中动态获取。

我找到的最接近的是a question about scnaf in Python,但略有不同。

【问题讨论】:

    标签: python-2.7 text-extraction string.format


    【解决方案1】:

    您正在寻找包parse,它被描述为:parse() 与 format() 相反。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      相关资源
      最近更新 更多