so, I was using tabulate to format my data in my latest project. But, it keeps giving this error and I'm not able to understand the reason behind it. Here's the code:
from datetime import date
from tabulate import tabulate
records = []
sno = [0,1]
jades = [0,1]
events = ['0',1]
dates = ['0',1]
for i in range(0, len(jades)):
records.append([sno[i], jades[i], events[i], dates[i]])
print(records)
record = records[-1]
print(record)
print(tabulate(record, headers=["Sno.", "Jades", "Event", "Date"], tablefmt='simple'))
here's the output:
[[0, 0, '0', '0'], [1, 1, 1, 1]]
[1, 1, 1, 1]
Traceback (most recent call last):
File "/home/kartik/Desktop/discord-bot/Rem.py", line 16, in <module>
print(tabulate(record, headers=["Sno.", "Jades", "Event", "Date"], tablefmt='simple'))
File "/home/kartik/.local/lib/python3.8/site-packages/tabulate.py", line 1426, in tabulate
list_of_lists, headers = _normalize_tabular_data(
File "/home/kartik/.local/lib/python3.8/site-packages/tabulate.py", line 1103, in _normalize_tabular_data
rows = list(map(list, rows))
TypeError: 'int' object is not iterable
Do note that the code which i posted is just the part which i found was causing the error. Those line numbers may differ.
I tried figure out myself but the only thing I could come up with was this code:
record = [records[-1]]
I converted that last records into list of its own.
Please, help me understand the reason behind it. I'm kind of a beginner at python so, my question might be dumb.
Thank you in advance!
According to the documentation, the first parameter to tabulate is an iterable of iterables or similar, like a spreadsheet. You need both rows and columns. What you passed in was just one list, so internally, it first grabs the first element ("1" in your case), thinks of that as a row, and then tries to iterate through each column, which is where the exception is raised.
When you wrap it in another set of brackets, like:
record = [records[-1]]
Then you're actually having [[1, 1, 1, 1]]
, which is one row (the outer bracket) containing four columns (the inner bracket).
I've never used this library, so I might have rows/columns switched here, but the concept is the same.