如何在txt文件中以表格格式显示数据
How to display data in tabular format on a txt file
我已经被这个 python 问题困扰了几个小时。我正在尝试弄清楚如何将上面可以手动输入的数据写入 txt 文件,使其显示在两行八列 table 中。 name_array中的内容应该是headers,data_array中的内容是实际的数据。
name = str(raw_input( "Enter the student's name: "))
medianScore = float(raw_input("Enter the median group score for quizzes:"))
indScore = float(raw_input("Enter the score of the individual quiz: "))
assignmentScore = float(raw_input("Enter the score of the assignment: "))
test1Score = float(raw_input("Enter the score of exam one: "))
test2Score = float(raw_input("Enter the score of exam two: "))
test3Score = float(raw_input("Enter the score of the final exam: "))
fileName = str(raw_input("Enter the name of the file you would like to create: "))
f = file(fileName + ".txt" , a)
finalScore = ((medianScore * .14) + (indScore * .14) + (assignmentScore * .12) + (test1Score * .15) +(test2Score * .20) + (test3Score * .25))
data_array = [name, finalScore, test3Score, test1Score, test2Score, assignmentScore, indScore, medianScore]
name_array = [ "Student", "Final Grade", "Final Exam", "Exam 1", "Exam 2", "Assignments", "Solo Quizzes", "Group Quizzes"]
你有没有尝试过类似的东西:
output_file = 'out.txt'
with open(output_file, 'r+') as file:
file.write('\t'.join(name_array) + '\n')
file.write('\t'.join(data_array) + '\n')
如果你只想输出一个类似 csv 的文件,你可以使用 csv
包:
import csv
writer = csv.writer(f, delimiter='\t')
writer.writerow(name_array)
writer.writerow(data_array)
它将输出:
Student Final Grade Final Exam Exam 1 Exam 2 Assignments Solo Quizzes Group Quizzes
asd 3.88 6 4 5 3 2 1
在此示例中使用 tab
s 作为分隔符,但您可以将其替换为您想要的任何字符。有关更多选项,请参阅 this documentation。
相反,如果您想要更易于阅读的内容,您可以使用 tabulate
包:
from tabulate import tabulate
f.write(tabulate([data_array], headers=name_array))
它将产生:
Student Final Grade Final Exam Exam 1 Exam 2 Assignments Solo Quizzes Group Quizzes
--------- ------------- ------------ -------- -------- ------------- -------------- ---------------
asd 3.88 6 4 5 3 2 1
有关格式化 table 的更多选项,请参阅 this documentation。
我已经被这个 python 问题困扰了几个小时。我正在尝试弄清楚如何将上面可以手动输入的数据写入 txt 文件,使其显示在两行八列 table 中。 name_array中的内容应该是headers,data_array中的内容是实际的数据。
name = str(raw_input( "Enter the student's name: "))
medianScore = float(raw_input("Enter the median group score for quizzes:"))
indScore = float(raw_input("Enter the score of the individual quiz: "))
assignmentScore = float(raw_input("Enter the score of the assignment: "))
test1Score = float(raw_input("Enter the score of exam one: "))
test2Score = float(raw_input("Enter the score of exam two: "))
test3Score = float(raw_input("Enter the score of the final exam: "))
fileName = str(raw_input("Enter the name of the file you would like to create: "))
f = file(fileName + ".txt" , a)
finalScore = ((medianScore * .14) + (indScore * .14) + (assignmentScore * .12) + (test1Score * .15) +(test2Score * .20) + (test3Score * .25))
data_array = [name, finalScore, test3Score, test1Score, test2Score, assignmentScore, indScore, medianScore]
name_array = [ "Student", "Final Grade", "Final Exam", "Exam 1", "Exam 2", "Assignments", "Solo Quizzes", "Group Quizzes"]
你有没有尝试过类似的东西:
output_file = 'out.txt'
with open(output_file, 'r+') as file:
file.write('\t'.join(name_array) + '\n')
file.write('\t'.join(data_array) + '\n')
如果你只想输出一个类似 csv 的文件,你可以使用 csv
包:
import csv
writer = csv.writer(f, delimiter='\t')
writer.writerow(name_array)
writer.writerow(data_array)
它将输出:
Student Final Grade Final Exam Exam 1 Exam 2 Assignments Solo Quizzes Group Quizzes
asd 3.88 6 4 5 3 2 1
在此示例中使用 tab
s 作为分隔符,但您可以将其替换为您想要的任何字符。有关更多选项,请参阅 this documentation。
相反,如果您想要更易于阅读的内容,您可以使用 tabulate
包:
from tabulate import tabulate
f.write(tabulate([data_array], headers=name_array))
它将产生:
Student Final Grade Final Exam Exam 1 Exam 2 Assignments Solo Quizzes Group Quizzes
--------- ------------- ------------ -------- -------- ------------- -------------- ---------------
asd 3.88 6 4 5 3 2 1
有关格式化 table 的更多选项,请参阅 this documentation。