Python:Typeerror:传递给 object.__format__ 的非空格式字符串 [英] Python : Typeerror : Non-empty format string passed to object.__format__
问题描述
我知道有人问过这个问题这里 但该解决方案对我不起作用.我正在使用 python 3.4
.
I know this question has been asked here but the solution does not work for me. I am using python 3.4
.
我的脚本中有以下格式:
I have the following formatting in my script :
print ( "\t {0:20} {1:<11} {2:<25} {3:11} {4:11} {5:>32}".format( files.name.split('/')[-1], sizeof_fmt(files.size),
str( formatted_timestamp ), files.owner,
files.version_id, files.etag ))
这适用于 python 2.7.x
.但是在 3.4
中我得到错误:
This works in python 2.7.x
. But in 3.4
I get the error:
File "test.py", line 3, in file_print
versionid, etag ))
TypeError: non-empty format string passed to object.__format__
我试过了:
print ( "\t {0:20} {1:<11} {2:<25} {3:11} {!s4:11s} {!s5:>32s}".format( files.name.split('/')[-1], sizeof_fmt(files.size),
str( formatted_timestamp ), files.owner,
files.version_id, files.etag ))
但我仍然遇到同样的错误.我什至将 versionid
和 etag
转换为字符串并最终得到相同的错误.有人可以向我解释一下吗?
But I still get the same error. I even converted the versionid
and etag
to strings and end up getting the same error. Can someone explain this to me?
Etag 看起来像这样 9893532233caff98cd083a116b013c0b
,versionid 是 None
Etag look like this 9893532233caff98cd083a116b013c0b
, versionid is None
推荐答案
你的一个参数是一个没有自己的 __format__()
方法的类型,所以 object.__format__()
代替.
One of your parameters is a type that doesn't have their own __format__()
method, so object.__format__()
is used instead.
object.__format__()
不支持任何格式选项,包括字段宽度和对齐方式,这就是您收到错误的原因.
object.__format__()
doesn't support any formatting options, including field widths and alignment, which is why you get the error.
先转换为字符串应该会有所帮助,但您确实需要将转换放在字段名称之后;而不是 {!s4:11s}
使用 {4!s:11s}
等:
Conversion to string first should help, but you do need to put your conversion after the field name; instead of {!s4:11s}
use {4!s:11s}
, etc:
print ( "\t {0:20} {1:<11} {2:<25} {3:11} {4!s:11s} {5!s:>32s}".format(
files.name.split('/')[-1], sizeof_fmt(files.size),
str(formatted_timestamp), files.owner,
files.version_id, files.etag))
这篇关于Python:Typeerror:传递给 object.__format__ 的非空格式字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!