【问题标题】:Python:non-empty format string passed to object.__format__Python:传递给 object.__format__ 的非空格式字符串
【发布时间】:2018-02-07 20:36:45
【问题描述】:

images_name = "%s/%03d_image.jpg" % (target, i)

我已将其格式化为 images_name = '{:03d_image.jpg}'.format((target, i))

你能指出错误吗?

也可以用更好的方式格式化吗?

format_str = ('%s: Step %d, Loss = %.2f (%.1f examples/sec; %.3f ' 'sec/batch)')

print(format_str % (datetime.now(), step, loss_value,examples_per_sec, duration))

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    您应该像使用旧格式样式一样指定单独的占位符,并且不要在花括号中包含不需要格式化的部分:

    images_name = '{}/{:03d}_image.jpg'.format(target, i)
    

    同样的规则适用于第二个字符串:

    format_str = '{}: Step {:d}, Loss = {:.2f} ({:.1f} examples/sec; {:.3f} sec/batch)'
    print(format_str.format(datetime.now(), step, 
                            loss_value, examples_per_sec, duration))
    

    阅读更多关于old and new format specifications in Python的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-19
      • 2017-04-04
      • 2017-12-30
      • 2021-10-31
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多