代码之家  ›  专栏  ›  技术社区  ›  Shiv Deepak

(xyz)的表示在python中是什么意思

  •  2
  • Shiv Deepak  · 技术社区  · 15 年前

    python中的这些表示是什么以及如何使用。我的猜测是它被用于数据验证,但永远无法使用它。

    %(name)s
    %(levelno)s
    %(levelname)s
    %(pathname)s
    %(process)d 
    etc..
    
    2 回复  |  直到 15 年前
        1
  •  6
  •   Fred Nurk    15 年前

    这是 string formatting using keys :

    >>> d = {"answer": 42}
    >>> "the answer is %(answer)d" % d
    'the answer is 42'
    
        2
  •  2
  •   unwind    15 年前

    它通过从字典中选取值来格式化字符串:

    test = { "foo": 48,  "bar": 4711, "hello": "yes" }
    print "%(foo)d %(bar)d and %(hello)s" % test
    

    印刷品:

    48 4711 and yes