Reputation:
I've been playing around with Python tabulate
module after found it here.
Instead of having separate box when reading it from file, would it be possible to combine/join it?
Here is the sample code and output.
wolf@linux:~$ cat file.txt
Apples
Bananas
Cherries
wolf@linux:~$
Python code
wolf@linux:~$ cat script.py
from tabulate import tabulate
with open(r'file.txt') as f:
for i,j in enumerate(f.read().split(), 1):
table = [[ i,j ]]
print(tabulate(table, tablefmt="grid"))
wolf@linux:~$
Output
wolf@linux:~$ python script.py
+---+--------+
| 1 | Apples |
+---+--------+
+---+---------+
| 2 | Bananas |
+---+---------+
+---+----------+
| 3 | Cherries |
+---+----------+
wolf@linux:~$
Desired Output
wolf@linux:~$ python script.py
+---+----------+
| 1 | Apples |
+---+----------+
| 2 | Bananas |
+---+----------+
| 3 | Cherries |
+---+----------+
wolf@linux:~$
Upvotes: 2
Views: 1647
Reputation: 21275
You should create a single table & print that instead of creating table
3 times & printing each:
from tabulate import tabulate
with open(r'temp.txt') as f:
table = []
for i,j in enumerate(f.read().split(), 1):
table.append([ i,j ])
print(tabulate(table, tablefmt="grid"))
Result:
+---+----------+
| 1 | Apples |
+---+----------+
| 2 | Bananas |
+---+----------+
| 3 | Cherries |
+---+----------+
Upvotes: 1