簡體   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