0

I'm trying to insert a SEPARATING_LINE in a tabulate in Python.

The example with lists works perfectly:

import tabulate

print(tabulate.tabulate([["A", 200], ["B", 100], tabulate.SEPARATING_LINE, ["Total", 4]], headers=["Col1", "Col2"]))

yields:

Col1 Col2

------ ------

A 200

B 100

------ ------

Total 4

However while this works:

print(
    tabulate.tabulate(
        [
            {"Col1": "A", "Col2": 200},
            {"Col1": "B", "Col2": 100},
            #tabulate.SEPARATING_LINE,
            {"Col1": "TOTAL", "Col2": 200},
        ],
        headers="keys",
    )
)

yielding:

Col1 Col2

------ ------

A 200

B 100

TOTAL 200

I cannot insert the tabulate.SEPARATING_LINE as is:

Traceback (most recent call last):
  File "/home/raul/python-playground/tabulates1.py", line 8, in <module>
    tabulate.tabulate(
  File "/home/raul/.venv/lib/python3.11/site-packages/tabulate/__init__.py", line 2048, in tabulate
    list_of_lists, headers = _normalize_tabular_data(
                             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/raul/.venv/lib/python3.11/site-packages/tabulate/__init__.py", line 1409, in _normalize_tabular_data
    for k in row.keys():
             ^^^^^^^^
AttributeError: 'str' object has no attribute 'keys'

Is tabulate.SEPARATING_LINE actually? supported with lists of dicts? Thanks.

I expected to get in the dict-based tabulate the same result as in the list-based one: a separating line (a line with dashes).

Raúl Salinas-Monteagudo
  • 3,412
  • 1
  • 24
  • 22

1 Answers1

0

I found out how to do it: I have to use the SEPARATING_LINE constant with ANY existing column name (a non-existing one will NOT work, BTW ).

{"Col1": tabulate.SEPARATING_LINE},
Raúl Salinas-Monteagudo
  • 3,412
  • 1
  • 24
  • 22