0

Tried a code for having a table in output but the table is not organized enough

This is the code that I used for having an output table:

import tabulate
from tabulate import tabulate
data = [[1, 'Zero', "%0.4f" % stdvzero3, "%0.4f" % stdavgzero3],
[2,'Std A', "%0.3f" % stdva3, "%0.3f" % stdavga3],
[3,'Std B', "%0.3f" % stdvb3, "%0.3f" % stdavgb3],
[4,'Std C', "%0.3f" % stdvc3, "%0.3f" % stdavgc3],
[5,'Std D', "%0.3f" % stdvd3, "%0.3f" % stdavgd3],
[6,'Std E', "%0.3f" % stdve3, "%0.3f" % stdavge3 ],
[7,'Std F', "%0.3f" % stdvf3, "%0.3f" % stdavgf3]],
print (tabulate(data, headers=["No", "Type", "STD_N2O", "Mean_N2O"], tablefmt='simple'))

and this is the table that I got. Can you please let me know how to make it in line and tidy ?

No                                 Type                                 STD_N2O                              Mean_N2O
---------------------------------  ---------------------------------  -----------------------------------  ---------------------------------  -----------------------------------  -----------------------------------  -----------------------------------
[1, 'Zero', '0.1027', '387.4733']  [2, 'Std A', '27.756', '546.123']  [3, 'Std B', '173.254', '4608.491']  [4, 'Std C', '52.377', '847.775']  [5, 'Std D', '170.961', '1623.217']  [6, 'Std E', '491.289', '2938.067']  [7, 'Std F', '173.254', '4608.491']
YaOzI
  • 16,128
  • 9
  • 76
  • 72
Zahra
  • 1

1 Answers1

0

There is a bug in your code, the last comma make data become a tuple

data = [[1, 'Zero', "%0.4f" % stdvzero3, "%0.4f" % stdavgzero3],
[2,'Std A', "%0.3f" % stdva3, "%0.3f" % stdavga3],
[3,'Std B', "%0.3f" % stdvb3, "%0.3f" % stdavgb3],
[4,'Std C', "%0.3f" % stdvc3, "%0.3f" % stdavgc3],
[5,'Std D', "%0.3f" % stdvd3, "%0.3f" % stdavgd3],
[6,'Std E', "%0.3f" % stdve3, "%0.3f" % stdavge3 ],
[7,'Std F', "%0.3f" % stdvf3, "%0.3f" % stdavgf3]],

>>> type(data)
tuple

Reformat your code, not only make your code more readable but also help you locate potential bug easier:

data = [
    [1, 'Zero', "%0.4f" % 9.999, "%0.4f" % 6.666],
    [2,'Std A', "%0.3f" % 9.999, "%0.3f" % 6.666],
    [3,'Std B', "%0.3f" % 9.999, "%0.3f" % 6.666],
    [4,'Std C', "%0.3f" % 9.999, "%0.3f" % 6.666],
    [5,'Std D', "%0.3f" % 9.999, "%0.3f" % 6.666],
    [6,'Std E', "%0.3f" % 9.999, "%0.3f" % 6.666],
    [7,'Std F', "%0.3f" % 9.999, "%0.3f" % 6.666],
]

>>> type(data)
list

>>> print(tabulate(data, headers=["No", "Type", "STD_N2O", "Mean_N2O"], tablefmt='simple'))
  No  Type      STD_N2O    Mean_N2O
----  ------  ---------  ----------
   1  Zero        9.999       6.666
   2  Std A       9.999       6.666
   3  Std B       9.999       6.666
   4  Std C       9.999       6.666
   5  Std D       9.999       6.666
   6  Std E       9.999       6.666
   7  Std F       9.999       6.666
YaOzI
  • 16,128
  • 9
  • 76
  • 72