随笔 - 328  文章 - 1 评论 - 5 阅读 - 30万
< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

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   Go_Forward  阅读(347)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示