【问题标题】:TypeError: unsupported format string passed to tuple.__format__TypeError:不支持的格式字符串传递给 tuple.__format__
【发布时间】:2020-11-03 22:59:23
【问题描述】:

我想显示这个:


号码名称

1 简

2 琳达

3 弗拉基米尔


但是当我将 row 放在最后一行时,它给了我一个错误。

output = cursor.fetchall()

for row in output:
    print("{0:20}\t{1:20}".format("Number", "Name"))
    print("{0:20}\t{1:20}".format(row, row[0]))

【问题讨论】:

  • 这里需要enumerate,并且需要将第一个打印件拉出循环。

标签: python


【解决方案1】:

根据我有限的测试,Python 的 f-strings 和 .format(用 3.8 测试)不支持像 "{:2}".format([0]) 这样的字符串(即带有列表类型的参数)。如果您确实需要打印一份列表,请将其转换为str

rows = [[1, "Jane"], [2, "Linda"], [3, "Vladimir"]]
print("{0:16}{1:8}".format("Number", "Name"))
for row in rows:
    print("{0:<16}{1:8}".format(str(row), row[1]))

或者只是单独传递每个元素:

rows = [[1, "Jane"], [2, "Linda"], [3, "Vladimir"]]
print("{0:8}{1:8}".format("Number", "Name"))
for row in rows:
    print("{0:<8}{1:8}".format(row[0], row[1]))

【讨论】:

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