这是我尝试的
for book in books:
print (tabulate(book, headers=["ID", "Title", "Author", "Pub_year", "available", "Shelf Place"]))
输出为
您没有显示有问题的output
(注释中的文本无法读取)。你没有显示你在books
中有什么,所以我猜所有的问题是tabulate
需要行列表(每行都必须是项目列表),但你发送单行,它线程这个列表中的每个字符串作为项目列表(所以它将每个字符作为分隔项)。
如果你想显示所有的书,那么你应该直接使用books
而不是循环使用book
如果你想在单独的表中显示每个book
,那么使用list[book]
而不是book
from tabulate import tabulate
books = [
['1','Title 1','Author 1','2009', True, '9.99'],
['2','Title 2','Author 2','2010', True, '19.99'],
['3','Title 3','Author 3','2011', True, '29.99'],
]
print(tabulate(books, headers=["ID", "Title", "Author", "Pub_year", "available", "Shelf Place"]))
print('=================')
for book in books:
print(tabulate([book], headers=["ID", "Title", "Author", "Pub_year", "available", "Shelf Place"]))
print('=================')
结果:
ID Title Author Pub_year available Shelf Place
---- ------- -------- ---------- ----------- -------------
1 Title 1 Author 1 2009 True 9.99
2 Title 2 Author 2 2010 True 19.99
3 Title 3 Author 3 2011 True 29.99
=================
ID Title Author Pub_year available Shelf Place
---- ------- -------- ---------- ----------- -------------
1 Title 1 Author 1 2009 True 9.99
=================
ID Title Author Pub_year available Shelf Place
---- ------- -------- ---------- ----------- -------------
2 Title 2 Author 2 2010 True 19.99
=================
ID Title Author Pub_year available Shelf Place
---- ------- -------- ---------- ----------- -------------
3 Title 3 Author 3 2011 True 29.99
=================