4

Id like to know if there is some argument I can pass to tabulate module and specify the quantity of empty spaces between columns.

I can see there are 2 spaces by default but I didnt find how do manipulate that. This is script example.

from tabulate import tabulate

data = [
  ["Name","LastName", "Age"],
  ["Juan","Lopez", "22"],
  ["Luisa","Perez", "24"],
  ["Ana","Sanchez", "23"]
]

print (tabulate(data, stralign="right", tablefmt="plain"))

Output:

 Name  LastName  Age
 Juan     Lopez   22
Luisa     Perez   24
  Ana   Sanchez   23

The complete task is about extract data from a plan text and organize it. One way I did to solve the problem was adding empty columns between each data column but maybe is not the most eficcient way.

enrique-es
  • 41
  • 1
  • 5

3 Answers3

2

As far as I can tell, tabulate doesn't seem to have an argument for this. Maybe look into prettytable, and especially the "Changing the appearance of your table - the hard way" chapter, that allows you to be more flexible with the table style.

EDIT: texttable may be useful too, as you can define the column width. The documentation seems to be a bit lacking though, and while the ease of use may be better, the modularity seems to be a bit worse at first glance.

GregoirePelegrin
  • 1,206
  • 2
  • 7
  • 23
0

I was able to reduce the column separator width to a single character with:

tablefmt = "minpadding"
tabulate._table_formats[tablefmt] = tabulate.TableFormat(
  lineabove=tabulate.Line("", "-", " ", ""),
  linebelowheader=tabulate.Line("", "-", " ", ""),
  linebetweenrows=None,
  linebelow=tabulate.Line("", "-", " ", ""),
  headerrow=tabulate.DataRow("", " ", ""),
  datarow=tabulate.DataRow("", " ", ""),
  padding=0,
  with_header_hide=["lineabove", "linebelow"],
)
tabulate.multiline_formats[tablefmt] = tablefmt
tabulate.tabulate_formats = list(sorted(tabulate._table_formats.keys()))
rows = [
    ["Cell 1", "Cell 2"],
    ["Cell 3", "Cell 4a\nCell 4b"],
    ["Cell 5", "Cell 6"],
]
print(tabulate.tabulate(rows, headers="firstrow", tablefmt=tablefmt))

which produces:

Cell 1   Cell 2
-------- --------
Cell 3   Cell 4a
         Cell 4b
Cell 5   Cell 6
Ross Smith II
  • 11,799
  • 1
  • 38
  • 43
-3

I know this is an older post, but could you not use whitespace to extend the cell?

Text formatting By default, tabulate removes leading and trailing whitespace from text columns. To disable whitespace removal, set the global module-level flag PRESERVE_WHITESPACE:

import tabulate tabulate.PRESERVE_WHITESPACE = True

Tabulate on PyPI

Shikeagle
  • 1
  • 1
  • 1
  • Well the question isn't about trailing spaces but a solution to customize column width. So things are better left the way they are in unless you have an alternate solution. Welcome to SO – Ratul Hasan May 19 '23 at 15:17
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 20 '23 at 06:53
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34414799) – Destroy666 May 22 '23 at 21:04