I have to sketch out the periodic table with ten elements through a list like the following one:
pt = [(1,"Hydrogen","H",1), (2,"Helium","He",4), ...].
As follows, the widths of the corresponding columns in the table and to the required alignment
(3 right 20 left 6 center 10 right).
The final results I should obtain look like this.
I have tried to create through the package tabulate
, following this procedure
pt = [(1,"Hydrogen","H",1.008), (2,"Helium","He",4.0026), (3, "Lithium", "Li", 6.94), (4, "Beryllium", "Be", 9.0122),
(5, "Boron", "B", 10.81), (6, "Carbon", "C", 12.011), (7, "Nitrogen", "N", 14.007), (8, "Oxygen", "O", 15.999),
(9, "Fluorine", "F", 18.998), (10, "Neon", "Ne", 20.180)]
head = ["No.", "Name", "Symbol", "Weight"]
print(tabulate(pt, headers=head, tablefmt="grid"))
The results is nice
but since I have to respect the width dimensions, as described above, I do not how to include them in the code. I was suggested to create the first three lines manually but lines with chemical elements should be created in the 'for' loop (for an item in pt:)
. Use methods for strings (rjust, center, ljust)
.
Is there anyone that knows how to do this?
CodePudding user response:
Since you use tabulate, there is a solution to modify your code by:
- Adding an extra row full of characters of specified size for each column at the end the array
- Calling
tabulate
including specification of the alignment usingcoalign
- Remove the extra row
Since the output is a string, the width of the columns is not associated with any measure e.g. in cm, so you will need to define a length scale that suits your needs, i.e. the number of characters per required width unit.
Note that 2 characters are removed from the computed width to account for the inner column margins which are 1 character left and right.
from tabulate import tabulate
pt = [(1,"Hydrogen","H",1.008), (2,"Helium","He",4.0026), (3, "Lithium", "Li", 6.94), (4, "Beryllium", "Be", 9.0122),
(5, "Boron", "B", 10.81), (6, "Carbon", "C", 12.011), (7, "Nitrogen", "N", 14.007), (8, "Oxygen", "O", 15.999),
(9, "Fluorine", "F", 18.998), (10, "Neon", "Ne", 20.180)]
head = ["No.", "Name", "Symbol", "Weight"]
lengthScale = 2
widths = [3, 20, 6, 10]
pt.append(["-"*(width*lengthScale-2) for width in widths])
tab = tabulate(pt, headers=head, tablefmt="grid", colalign=("right","left","center","right"))
for line in range(2):
tab = tab[:tab.rfind('\n')]
print(tab)
It produces:
------- ---------------------------------------- ------------ --------------------
| No. | Name | Symbol | Weight |
======= ======================================== ============ ====================
| 1 | Hydrogen | H | 1.008 |
------- ---------------------------------------- ------------ --------------------
| 2 | Helium | He | 4.0026 |
------- ---------------------------------------- ------------ --------------------
| 3 | Lithium | Li | 6.94 |
------- ---------------------------------------- ------------ --------------------
| 4 | Beryllium | Be | 9.0122 |
------- ---------------------------------------- ------------ --------------------
| 5 | Boron | B | 10.81 |
------- ---------------------------------------- ------------ --------------------
| 6 | Carbon | C | 12.011 |
------- ---------------------------------------- ------------ --------------------
| 7 | Nitrogen | N | 14.007 |
------- ---------------------------------------- ------------ --------------------
| 8 | Oxygen | O | 15.999 |
------- ---------------------------------------- ------------ --------------------
| 9 | Fluorine | F | 18.998 |
------- ---------------------------------------- ------------ --------------------
| 10 | Neon | Ne | 20.18 |
------- ---------------------------------------- ------------ --------------------