astanin / python-tabulate

Pretty-print tabular data in Python, a library and a command-line utility. Repository migrated from bitbucket.org/astanin/python-tabulate.
https://pypi.org/project/tabulate/
MIT License
2.08k stars 162 forks source link

Bug: SEPARATING_LINE does not work with headers="keys" #334

Open fuellbie opened 1 month ago

fuellbie commented 1 month ago

tabulate.SEPARATING_LINE does not work when the table consists of dictionaries and keys are used as headers:

>>> table = [{"name": "Ben"}, {"name": "Adam"}]
>>> print(tabulate.tabulate(table, headers="keys"))
name
------
Ben
Adam
>>> table = [{"name": "Ben"}, tabulate.SEPARATING_LINE, {"name": "Adam"}]
>>> print(tabulate.tabulate(table, headers="keys"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/fui4fe/.local/lib/python3.10/site-packages/tabulate/__init__.py", line 2048, in tabulate
    list_of_lists, headers = _normalize_tabular_data(
  File "/home/fui4fe/.local/lib/python3.10/site-packages/tabulate/__init__.py", line 1409, in _normalize_tabular_data
    for k in row.keys():
AttributeError: 'str' object has no attribute 'keys'

Expected behaivour:

>>> table = [{"name": "Ben"}, tabulate.SEPARATING_LINE, {"name": "Adam"}]
>>> print(tabulate.tabulate(table, headers="keys"))
name
------
Ben
------
Adam