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

coltype autodetection assumes "0.0" is a float and the table shows "0", instead of the string "0.0" #299

Open gbus opened 9 months ago

gbus commented 9 months ago

I couldn't find a way to enforce a column type to solve this issue: Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux

>>> t = [["First version", "0.0"], ["Second version", "1.0"], ["Third version", "1.1"]]
>>> print(tabulate(t))
--------------  ---
First version   0
Second version  1
Third version   1.1
--------------  ---

I understand that it is not common to represent 0.0 as string, but I have the unusual requirement to printout versions and the auto-recognition of the column type cuts the ".0". Is there a way to enforce tabulate to keep it as string?

coltonpaquette commented 9 months ago

Setting disable_numparse to True will give you the result you are looking for:

>>> t = [["First version", "0.0"], ["Second version", "1.0"], ["Third version", "1.1"]]
>>> print(tabulate(t, disable_numparse=True))
--------------  ---
First version   0.0
Second version  1.0
Third version   1.1
--------------  ---