【问题标题】:String formatting error字符串格式错误
【发布时间】:2011-01-27 20:32:47
【问题描述】:

在 Python 3.1.1 中使用代码 print('{0} is not'.format('That that is not')),我收到以下错误:

AttributeError: 'str' object has no attribute 'format'

当我删除 Netbeans 开头自动插入的行时:

from distutils.command.bdist_dumb import format

它本身会导致错误

ImportError: cannot import name format

我在这里做错了什么?

【问题讨论】:

    标签: python string format


    【解决方案1】:

    您必须运行旧版本的 Python。这在 Python 3.1.1+ 中确实有效:

    $ python3
    Python 3.1.1+ (r311:74480, Nov  2 2009, 14:49:22) 
    [GCC 4.4.1] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> '{0} is not'.format('That that is not')
    'That that is not is not'
    

    但是,您会在 Python 2.5.4 中遇到此错误:

    $ python2.5
    Python 2.5.4 (r254:67916, Jan 20 2010, 21:44:03) 
    [GCC 4.4.1] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> '{0} is not'.format('That that is not')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'str' object has no attribute 'format'
    

    此功能似乎已向后移植到 Python 2.6,因此您不会在此处收到此错误。您必须运行 Python

    【讨论】:

    • 奇怪...我在 Netbeans 中运行
    • 您应该能够配置您的 Netbeans 项目使用的 Python 版本:wiki.netbeans.org/…
    • 重新投票:谢谢。如果你明天再试一次,它可能会起作用。我相信 SO 有每日限制,而不是终身限制。
    • 谢谢!我终于让 Netbeans 以我想要的方式工作了 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    相关资源
    最近更新 更多