显示;用Python分离表格格式的值
我拥有以下函数,从结果选择结果。txt文件并将其显示给用户,第一行出现得很好,但其余的结果并不完善。我不确定是什么原因引起的。 功能
def show_result():
'''prints the score list'''
file_exist()
print_whole_list = []
print("Results")
print("*"*41)
result = open("results.txt","r")
res = result.readlines()
print("Name: Lap1: Lap2: Lap3: In total: Average:")
for i in res:
temp_list = i.split(";")
total = int(temp_list[1]) + int(temp_list[2]) + int(temp_list[3])
average = int(total) / 3.0
average = round(average, 2)
temp_list.insert(4, total)
temp_list.insert(5, average)
print_whole_list.extend(temp_list)
for row in print_whole_list:
print("{0:{width}}".format(row, width=10), end=' ')
result.close()
这是文本文件中的记录
Kembos;23;43;23;
Moses;42;51;43;
Ben;43;23;21;
:
I'm having the following function which picks results from a results.txt file and displays it to the user, the first row comes out nicely but the rest of the results are not well-aligned. I'm not sure what's causing this. here's the function
def show_result():
'''prints the score list'''
file_exist()
print_whole_list = []
print("Results")
print("*"*41)
result = open("results.txt","r")
res = result.readlines()
print("Name: Lap1: Lap2: Lap3: In total: Average:")
for i in res:
temp_list = i.split(";")
total = int(temp_list[1]) + int(temp_list[2]) + int(temp_list[3])
average = int(total) / 3.0
average = round(average, 2)
temp_list.insert(4, total)
temp_list.insert(5, average)
print_whole_list.extend(temp_list)
for row in print_whole_list:
print("{0:{width}}".format(row, width=10), end=' ')
result.close()
The records in the text file:
Kembos;23;43;23;
Moses;42;51;43;
Ben;43;23;21;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用表中的表格填充数据
以上的Python中的表格,将提供以下输出:
<” img src =“ https://i.sstatic.net/xpgwh.png” alt =“在此处输入图像说明”>
You can use tabulate instead to populate your data in a tabular format in python
Above code will give the following output: