I'm forming a BeautifulTable table from dict, whose keys are string sets of digits (e.g. "21608", "32099", "02978"). The keys of the dict are to become the first column of my table:
for (key, value) in stations.items():
table.rows.append([key] value) # 'value' is a list, so here I just concatenate [key] and [value1, ..., valueN] to form new list [key, value1, ..., valueN] to add to the table
The problem occurs when I try to print out (to stdout or a txt file) the table using print(table) command.
for (key, value) in stations.items():
table.rows.append([key] value)
print(table)
And the problem is: all the first column's sets of digits, that are starting with "0" (e.g. "02978", "02186"), are modified so that the first "0" gets stripped.
Then I tried to print out rows one by one convering them to list just after I append them to table:
for (key, value) in stations.items():
table.rows.append([key] value)
print(list(table.rows[-1]))
This output shows data as needed, with no zeroes stripped:
Why this result? The reason is in repr() method of BeautifulTable tables? I don't quite understand why whould it modify in any way the string types during the output. Any ideas, how could I avoid it?
CodePudding user response:
It's being converted into an int/float
The current behaviour is that, if a string can be parsed as a float/int, it will be parsed as a float/int
>>> import beautifultable
>>> table = beautifultable.BeautifulTable()
>>> table.append_row(['foo', '012345'])
>>> print(table)
----- -------
| foo | 12345 |
----- -------
You can use detect_numerics=False
to disable this behaviour.
>>> table = beautifultable.BeautifulTable(detect_numerics=False)
>>> table.append_row(['foo', '012345'])
>>> print(table)
----- --------
| foo | 012345 |
----- --------