【问题标题】:How to escape character '.' in python string replacement via the operator %如何转义字符“。”在 python 字符串替换中通过操作符 %
【发布时间】:2011-02-24 08:26:24
【问题描述】:

我想做以下事情

"@file %(unitname).C" % {'unitname':'Test'}

但它抱怨'.C'。我怎样才能逃脱'。性格?

【问题讨论】:

    标签: python string formatting


    【解决方案1】:

    你没有。你修复格式说明符。

    "@file %(unitname)s.C" % {'unitname':'Test'}
    

    【讨论】:

      【解决方案2】:

      另一种选择是,

      "@file {unitname}.C".format(unitname='Test')
      

      这可能更可取,因为format() 将成为 Python 3 中的新标准。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-17
        • 2015-10-14
        相关资源
        最近更新 更多