繁体   English   中英

我想在行中打印表格数据,但它在列中打印

[英]I want to print tabulated data in row but it is printing in column

这是我尝试过的

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
=================

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM