Bug: intfmt error for strings which contain integer values
Description
The argument intfmt throws an error when some value in the data is a string, but it looks like an integer. For example: the string value “1234” is considering for the _format function as an integer, but it is a string.
Not works:
print(tabulate([("1", 9200), ("2", 90000)], intfmt=","))
Works, but not the optimal solution when have plenty of data:
print(tabulate([("1", 9200), ("2", 90000)], intfmt=("", ",")))
Error
Traceback (most recent call last):
File "…/main.py", line 58, in <module>
main()
File "…/main.py", line 40, in main
print(tabulate([("1", 9200), ("2", 90000)], intfmt=","))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "…/python3.11/site-packages/tabulate/__init__.py", line 2156, in tabulate
cols = [
^
File "…/python3.11/site-packages/tabulate/__init__.py", line 2157, in <listcomp>
[_format(v, ct, fl_fmt, int_fmt, miss_v, has_invisible) for v in c]
File "…/python3.11/site-packages/tabulate/__init__.py", line 2157, in <listcomp>
[_format(v, ct, fl_fmt, int_fmt, miss_v, has_invisible) for v in c]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "…/python3.11/site-packages/tabulate/__init__.py", line 1222, in _format
return format(val, intfmt)
^^^^^^^^^^^^^^^^^^^
ValueError: Cannot specify ',' with 's'.
Debugging
I added the follow prints to debug the error.
def _format(val, valtype, floatfmt, intfmt, missingval="", has_invisible=True):
"""
…
""" # noqa
print("_format(val, valtype, …) | Line #1201")
print(f'val: {val} ({type(val)})')
print(f'valtype: {valtype}')
if val is None:
return missingval
Bug:
intfmt
error for strings which contain integer valuesDescription
The argument
intfmt
throws an error when some value in the data is a string, but it looks like an integer. For example: the string value “1234” is considering for the_format
function as an integer, but it is a string.Examples
Works:
print(tabulate([["a", 9200], ["b", 90000]], intfmt=","))
Not works:
print(tabulate([("1", 9200), ("2", 90000)], intfmt=","))
Works, but not the optimal solution when have plenty of data:
print(tabulate([("1", 9200), ("2", 90000)], intfmt=("", ",")))
Error
Debugging
I added the follow prints to debug the error.
When it works
print(tabulate([["a", 9200], ["b", 90000]], intfmt=","))
When it doesn't work
print(tabulate([("1", 9200), ("2", 90000)], intfmt=","))
Noticed that the
val
argument is a string, but thevaltype
is an integer. So, if it tries to execute the:It will throw an error because it is a string trying to format as an integer.