python Tabulate library: TypeError: zip_longest argument #1 must support iteration

python Tabulate library: TypeError: zip_longest argument #1 must support iteration

我有这本字典:{'id': 1, 'inner': {'test1': 1, 'test2': 2}} ,我想将其制成表格,但出现以下异常:

>>> from tabulate import tabulate
>>> d = {'id': 1, 'inner': {'test1': 1, 'test2': 2}}
>>> tabulate(d)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../env/lib/python3.6/site-packages/tabulate.py", line 1529, in tabulate
    tabular_data, headers, showindex=showindex
  File ".../env/lib/python3.6/site-packages/tabulate.py", line 1089, in _normalize_tabular_data
    izip_longest(*tabular_data.values())
TypeError: zip_longest argument #1 must support iteration

我不明白为什么我会遇到这种情况,以及如何确保这种情况不会再次发生。我的字典是我的服务器答案,它的模式可以很容易地改变,这种格式应该支持一切(!),所以我正在寻找一个通用的解决方案。我知道并非每个模式都可以格式化为 table,但这是用户的选择。

版本: 制表=0.8.9 python=3.6.9

根据文档:

The following tabular data types are supported:

  1.list of lists or another iterable of iterables
  2.list or another iterable of dicts (keys as columns)
  3.dict of iterables (keys as columns)
  4.two-dimensional NumPy array
  5.NumPy record arrays (names as columns)
  6.pandas.DataFrame

如果你想使用字典,它必须是 dict of iterable,这意味着你的例子是

{'id': [1], 'inner': {'test1': [1], 'test2': [2]}}