我正试图利用python库tabulate
将underground对象转换为表。
count = {
"George": 1,
"John": 2
}
下方显示的简单py线
amount = tabulate(count, tablefmt='html', headers=["User","Amount Issued"])
然而,我收到了错误:
Result: Failure
Exception: TypeError: 'int' object is not iterable
我的编程新手告诉我,整数作为值是不期望的。
我以为这行会生成:
用户 | 发行金额 | |
---|---|---|
George | 1 | |
John | 2 |
这是一个对我有用的片段!
count = [
{
"user": "George",
"amount_issued": 1
}, {
"user": "John",
"amount_issued": 2
}
]
amount = tabulate(count, tablefmt='html', headers={"User": "", "Amount Issued": ""})
它给出错误,因为他发现count
数据类型(dict(和header
数据类型(array(之间不一致。
我认为这个解决方案更可读,因为从技术上讲,John和George是两个对象,但您也可以尝试使用:
count = [["George", 1], ["John", 2]]
amount = tabulate(count, tablefmt='html', headers=["User","Amount Issued"])
其中count是数组的数组!它只是取决于以下哪种方法是检索数据的最简单方法。
使用dict
标头是关键,因此您需要类似的东西
count = {
"User": ["George", "John"],
"Amount Issued": [1, 2]
}