ValueError: headers for a list of dicts is not a dict or a keyword

from: https://bitbucket.org/astanin/python-tabulate/issues/39/valueerror-headers-for-a-list-of-dicts-is

 

The order of keys in a Python dictionary is undefined in Python 3. So the order of columns in a table given as a list of dicts is not determined either. Thus, if headers are just a sequence of values and data is a list of dicts, there is no sure and portable way to decide what columns the headers correspond to. So tabulate raises a ValueError which asks to use a dict of headers or keywords.

Correct usage with a list of dicts:

tabulate([{'foo': 17, 'bar': 42}], headers={'foo': 'Foo Value', 'bar': 'Bar Count'})

 

An update. To use dictionary keys as headers, supply headers="keys":

>>> traffic_data = [{"hex":"86da32", "squawk": "0000", "flight": "", "lat": 0, "lon": 0}]
>>> print tabulate(traffic_data, headers="keys")
lat flight squawk hex lon
----- -------- -------- ------ -----
0 0000 86da32 0

posted on 2019-10-11 14:52  Go_Forward  阅读(314)  评论(0编辑  收藏  举报