Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify the amount of spaces between columns by using tabulate module in python

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.

like image 935
enrique-es Avatar asked Oct 19 '25 09:10

enrique-es


1 Answers

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.

like image 168
GregoirePelegrin Avatar answered Oct 22 '25 05:10

GregoirePelegrin