
[英]python 2.7, 3.x doctest compatibility vs FileNotFoundError
[英]String-format placeholders for maximum compatibility with python 2.6, 2.7 and 3.x
我应该将哪些占位符与字符串的.format()
方法结合使用,以便在与python解释器的.format()
和3.x(在我的情况下为3.2)结合使用时兼容?
我开始使用{:}
占位符,但这不适用于2.6。 现在,我正在使用{<index-of-format-tuple>}
,它似乎适用于所有列出的版本。
有更好的选择吗? 性能不是问题,兼容性是问题。
这是Python 2.6中的字符串格式手册: https : //docs.python.org/2.6/library/string.html#format-string-syntax
应该毫无问题地支持其中的所有内容。
我个人通常选择C风格的字符串格式: https : //docs.python.org/2/library/stdtypes.html#string-formatting-operations
在大多数情况下,它工作得很好,并且对我来说更熟悉。 而且它仍然支持相当高级的格式:
>>> print '%(language)s has %(number)03d quote types.' % \
... {"language": "Python", "number": 2}
Python has 002 quote types.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.