简体   繁体   中英

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

here is what I tried

for book in books:
    print (tabulate(book, headers=["ID", "Title", "Author", "Pub_year", "available", "Shelf Place"]))

the output is

You didn't show output in question (text in comment is unreadable). And you didn't show what you have in books so I guess all problem is that tabulate needs list of rows (and every row has to be list of items) but you send single row and it threads every string in this list as list of items (so it treads every char as separated item).

If you want to display all books then you should use directly books instead of loop with book

And if you want to display every book in separated table then use list [book] instead of 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('=================')

Result:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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