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.1k stars 163 forks source link

problem aligning floats with trains zeros #249

Open varioustoxins opened 1 year ago

varioustoxins commented 1 year ago

I am trying to format a table with floating point numbers with a fixed number of trailing decimals including zeros (including redundant zeros)

so I want something like

#NUM  AA     HA      HN      N15      CA      CB        CO
1     A   4.320   0.000  125.740  52.270  18.340   179.080
2     G   0.000   8.310  107.940  44.770   0.000   179.920

however, if I add the numbers to the rows as floats this doesn't seem to work even if I specify floatfmt='7.3' and numalign ='decimals'

as this gives:

#NUM  AA  HA     HN      N15      CA      CB      CO
1     A   4.320  0.000   125.740  52.270  18.340  179.080
2     G   0.000  8.310   107.940  44.770  0.000   179.920

note the badly aligned zeros in row 2 column CB

so I end up using format(shift_value, '7.3f') to convert the numbers to strings before I append them to the rows in. the table and

HEADINGS='#NUM  AA  HA     HN      N15      CA      CB      CO'.split()
COL_ALIGN = ['right',] * len(HEADINGS)
COL_ALIGN[0] = 'left'
COL_ALIGN[1] = 'left'
tabulate(table, tablefmt="plain",  colalign=COL_ALIGN')

to tabulate the table

is there a better way, is the zeros problem a bug?