我想显示这个:
编号名称
1 Jane
2 Linda
3弗拉基米尔
但当我把行放在最后一行时,它给了我一个错误。
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]))
根据我的有限测试,Python的f-string和.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]))