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

Question:

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:

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.

Asked By: geojoe

||

Answers:

When using a dict headers are the keys, so you would need something like

count = {
    "User": ["George", "John"],
    "Amount Issued": [1, 2]
}
Answered By: Vorkos

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!

Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.