1

I am using texttable to print a table, but the data I want to print just happens to have some strings which look a bit like numbers. How do I stop texttable from interpreting those as floating points? I didn't pass them as floating points, so it is bizarre behaviour. I tried prepending spaces but it seems anything that can parse as a float counts as a float.

#!/bin/env python3.9

import texttable

table = texttable.Texttable()
table.add_rows([["Name", "Age", "Nickname"], ["Mr\nXavier\nHuon", 32, "Xav'"],
                ["Mr\nBaptiste\nClement", 1, "Baby"],
                ["Mme\nLouise\nBourgeau", "999994840", "Lou\n\nLoue"]])
print(table.draw() + "\n")

prints:

+----------+-----------+----------+
|   Name   |    Age    | Nickname |
+==========+===========+==========+
| Mr       | 32        | Xav'     |
| Xavier   |           |          |
| Huon     |           |          |
+----------+-----------+----------+
| Mr       | 1         | Baby     |
| Baptiste |           |          |
| Clement  |           |          |
+----------+-----------+----------+
| Mme      | 1.000e+09 | Lou      |
| Louise   |           |          |
| Bourgeau |           | Loue     |
+----------+-----------+----------+

but should print:

+----------+-----------+----------+
|   Name   |    Age    | Nickname |
+==========+===========+==========+
| Mr       | 32        | Xav'     |
| Xavier   |           |          |
| Huon     |           |          |
+----------+-----------+----------+
| Mr       | 1         | Baby     |
| Baptiste |           |          |
| Clement  |           |          |
+----------+-----------+----------+
| Mme      | 999994840 | Lou      |
| Louise   |           |          |
| Bourgeau |           | Loue     |
+----------+-----------+----------+
TamaMcGlinn
  • 2,840
  • 23
  • 34
  • Take a look at this [question](https://stackoverflow.com/questions/16362093/python-converts-long-number-to-float-i-believe-example-x-xxxeyy) – nerap Jun 18 '21 at 19:21
  • 1
    table.set_cols_dtype(['t', 't', 't']) should convert all your columns to text – Patrick Freitas Jun 18 '21 at 19:22

0 Answers0