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

Idea/Feature Request: Allow callable for `floatfmt`. #297

Open jagerber48 opened 10 months ago

jagerber48 commented 10 months ago

I have a custom formatter for floats. One example feature is that I can format floats to be in exponential notation but to always have an exponent that is a multiple of 3 (engineering notation). Right now if I apply this formatter to a table (e.g. list of lists) and then run it through tabulate, all of the floats (now strings) will be re-cast as floats and reformatted using the default float format. This is not what I want. In fact, because "float castable stings" get converted back to floats it's actually impossible for me to get what I want. What would be better is if I can pass in a callable function to be used to format floats instead of just a python built-in format specification mini-language specifier

Shreyansh-Agrawal commented 9 months ago

I am facing a similar issue while printing data using tabulate-

File "c:\\VSCode\QuizApp\controllers\handlers\quiz_handler.py", line 59, in display_questions_by_category
pretty_print( File "c:\\VSCode\QuizApp\utils\pretty_print.py", line 12, in pretty_print print(tabulate(data, headers=headers, tablefmt='rounded_grid', showindex=row_id)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\\Lib\site-packages\tabulate__init.py", line 2153, in tabulate cols = [ ^ File "C:\\Lib\site-packages\tabulate__init__.py", line 2154, in \ [_format(v, ct, fl_fmt, int_fmt, miss_v, has_invisible) for v in c] File "C:\\Lib\site-packages\tabulate__init__.py", line 2154, in \ [_format(v, ct, fl_fmt, int_fmt, miss_v, has_invisible) for v in c] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\\Lib\site-packages\tabulate\init__.py", line 1232, in _format return format(float(val), floatfmt) ^^^^^^^^^^ ValueError: could not convert string to float: 'True'

Type of data I am printing -

<class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'> <class 'str'>

nunamia commented 6 months ago

fix

image
jagerber48 commented 6 months ago

I was able to work around this issue fine by setting disable_numparse=True. I was pretty surprised to see this sort of auto-parsing done by default with an option to suppress this behavior slightly buried in the docs.