显示;用Python分离表格格式的值

发布于 2025-02-11 02:29:10 字数 895 浏览 3 评论 0原文

我拥有以下函数,从结果选择结果。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;

output

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁客 2025-02-18 02:29:10

您可以使用表中的表格填充数据

from tabulate import tabulate

result = open("results.txt","r")
res = result.readlines()
final = []
for line in res:
    temp_list = line.split(';')[:-1]
    total = int(temp_list[1]) + int(temp_list[2]) + int(temp_list[3])
    average = round(int(total) / 3.0, 2)
    temp_list.append(total)
    temp_list.append(average)
    final.append(temp_list)

print(tabulate(final, headers=["Name", "Lap1", "Lap2", "Lap3", "In Total", "Average"]))

以上的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

from tabulate import tabulate

result = open("results.txt","r")
res = result.readlines()
final = []
for line in res:
    temp_list = line.split(';')[:-1]
    total = int(temp_list[1]) + int(temp_list[2]) + int(temp_list[3])
    average = round(int(total) / 3.0, 2)
    temp_list.append(total)
    temp_list.append(average)
    final.append(temp_list)

print(tabulate(final, headers=["Name", "Lap1", "Lap2", "Lap3", "In Total", "Average"]))

Above code will give the following output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文