Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python tabulate TypeError: 'int' object is not iterable over object

I’m attempting to make use of the python library tabulate to convert the beneath object to a table.

count = {
"George": 1,
"John": 2
}

With the simple py line shown beneath

amount = tabulate(count, tablefmt='html', headers=["User","Amount Issued"])

However, I’m recieving the error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Result: Failure
Exception: TypeError: 'int' object is not iterable

My novice programming knowledge is telling me that the integers as values are not expected.

I assumed this line would generate:

User Amount Issued
George 1
John 2

I’m not sure how to convert the object values to type string without iterating through the values using a for loop and thought this was not the best method of doing so.

Any help is greatly appreciated.

>Solution :

This is a snippet that worked for me!

count = [
    {
        "user": "George",
        "amount_issued": 1
    }, {
        "user": "John",
        "amount_issued": 2
    }
]

amount = tabulate.tabulate(count, tablefmt='html', headers={"User": "", "Amount Issued": ""})

It gives error because he finds an inconsistency between the count data type (dict) and the header data type (array).

I think this solution is more readable, since John and George are technically two objects, but you could also try with:

count = [["George", 1], ["John", 2]]

amount = tabulate(count, tablefmt='html', headers=["User","Amount Issued"])

Where count is an array of arrays!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading