我一直在玩Pythontabulate
找到模块后here https://stackoverflow.com/questions/62876323/python-create-a-box-rectangle-table-outside-of-a-text/62876451.
从文件中读取时不需要单独的框,是否可以组合/加入它?
这是示例代码和输出。
wolf@linux:~$ cat file.txt
Apples
Bananas
Cherries
wolf@linux:~$
Python代码
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:~$
所需输出
wolf@linux:~$ python script.py
+---+----------+
| 1 | Apples |
+---+----------+
| 2 | Bananas |
+---+----------+
| 3 | Cherries |
+---+----------+
wolf@linux:~$
您应该创建一个表并打印它而不是创建table
3 次并每次打印:
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 |
+---+----------+
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)