-2

here is what I tried

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

the output is

TDG
  • 5,909
  • 3
  • 30
  • 51
  • I'm curious - what is the output? – TDG Jun 26 '22 at 09:21
  • 1 ID Title Author Pub_year available Shelf Place ---- ------- -------- ---------- ----------- ------------- 0 0 0 0 1 P y t h o n J i m c a 2 0 2 0 1 2 2 0 B 2 – James Demisse Jun 26 '22 at 10:57
  • I don't see output in your question. Don't put it in comment. In comment it is unreadable. – furas Jun 26 '22 at 14:50
  • maybe you should send `books` directly ot `tabulate`. it may need list of rows (even every row is list of items) but you send single row and it treats every string in this row as list of items and it may display every char as separated value. And if you really want to display single row then use list `[book]` instead of `book` – furas Jun 26 '22 at 14:51

1 Answers1

0

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
=================
furas
  • 134,197
  • 12
  • 106
  • 148