Python:均匀间隔具有不同字符串长度的输出数据 [英] Python: Evenly space output data with varying string lengths
问题描述
我试图让我的输出数据看起来像这样:
-------------------------------------------------------课程成绩报告-------------------------------------------------------雅各布森,马克 19.0 <--- 20,17,20斯努德,莫提穆尔 16.5 <--- 20,19,18,17,16,15,14,13卢森堡,罗莎 15.0 <--- 18,15,20,10,12阿塔纳索夫,约翰 20.0 <--- 20,20,20,20,20,20,20料斗,格蕾丝 20.0 <--- 20,20,20,20,20,20-------------------------------------------------------
但我不知道如何处理不同的名称长度.我的输出目前看起来像这样.
<小时> 课程成绩报告-------------------------------------------------------雅各布森,马克 19.0 <--- 20,17,20斯努德,莫提穆尔 16.5 <--- 20,19,18,17,16,15,14,13卢森堡,罗莎 15.0 <--- 18,15,20,10,12阿塔纳索夫,约翰 20.0 <--- 20,20,20,20,20,20,20料斗,格蕾丝 20.0 <--- 20,20,20,20,20,20-------------------------------------------------------
我编写的程序是获取成绩分数的输入文件并收集数据并整齐地打印出平均值.
输入文件如下所示:
马克·雅各布森,20,17,20Mortimur Snurd,20,19,18,17,16,15,14,13罗莎卢森堡,18,15,20,10,12约翰·阿塔纳索夫,20,20,20,20,20,20,20格蕾丝·霍珀,20,20,20,20,20,20
这是我的代码,它收集姓名和分数,并打印出包含姓氏、名字、平均分数的数据,然后是得出平均分数的实际分数.
file = input("请输入文件名:")Grade_file = 打开(文件,'r')打印()打印(' - - - - - - - - - - - - - - - - - - - - - - - -----------')print('\t\t课程成绩报告')打印(' - - - - - - - - - - - - - - - - - - - - - - - -----------')对于grade_file 中的文本:end_of_name = text.find(',')name_seperated = text.find(' ')first_name = text[0:name_seperated]last_name = text[name_seperated+1:end_of_name]name_last_first = last_name + "," + " " + first_name成绩 = 文本 [end_of_name+1:]开始 = 0指数 = 0sum_n = 0平均值 = 0分数 = 0计数 = 0而索引
我只需要弄清楚如何有偶数空格,以便像第一个输出一样生成偶数行和列.非常感谢帮助!谢谢!
一种方法是使用 Python 的内置 C 样式格式.请注意,负字段宽度表示该字段要左对齐:
<预><代码>>>>打印(%-30s %4.1f"%(雅各布森,马克",19.0))雅各布森,马克 19.0>>>或者,您可以使用字符串 format
方法:
您还可以使用格式化字符串文字(f-strings):
<预><代码>>>>name =雅各布森,马克">>>平均值 = 19.0>>>打印(f{name:30s} {average:4.1f}")雅各布森,马克 19.0>>>I am trying to get my output data to look like this:
-------------------------------------------------------
Grade Report for Programs
-------------------------------------------------------
Jacobson, Mark 19.0 <--- 20,17,20
Snurd, Mortimur 16.5 <--- 20,19,18,17,16,15,14,13
Luxemburg, Rosa 15.0 <--- 18,15,20,10,12
Atanasoff, John 20.0 <--- 20,20,20,20,20,20,20
Hopper, Grace 20.0 <--- 20,20,20,20,20,20
-------------------------------------------------------
But I don't know how to deal with the varying name length. My output currently looks like this.
Grade Report for Programs
-------------------------------------------------------
Jacobson, Mark 19.0 <--- 20,17,20
Snurd, Mortimur 16.5 <--- 20,19,18,17,16,15,14,13
Luxemburg, Rosa 15.0 <--- 18,15,20,10,12
Atanasoff, John 20.0 <--- 20,20,20,20,20,20,20
Hopper, Grace 20.0 <--- 20,20,20,20,20,20
-------------------------------------------------------
The program I have written is to take an input file of grade scores and collect the data and neatly print out the average.
The input file looks something like this:
Mark Jacobson,20,17,20
Mortimur Snurd,20,19,18,17,16,15,14,13
Rosa Luxemburg,18,15,20,10,12
John Atanasoff,20,20,20,20,20,20,20
Grace Hopper,20,20,20,20,20,20
And here is my code that collects the name and scores, and prints out the data with last name, first name, average score, then the actual scores that resulted to the average.
file = input("Enter filename: ")
grade_file = open(file, 'r')
print()
print('---------------------------------------------------------')
print('\t\tGrade Report for Programs')
print('---------------------------------------------------------')
for text in grade_file:
end_of_name = text.find(',')
name_seperated = text.find(' ')
first_name = text[0:name_seperated]
last_name = text[name_seperated+1:end_of_name]
name_last_first = last_name + "," + " " + first_name
grades = text[end_of_name+1:]
start = 0
index = 0
sum_n = 0
average= 0
score = 0
count = 0
while index < len(grades):
if grades[index] == ',':
score = int(grades[start:index])
count += 1
sum_n = score + sum_n
start = index + 1
index += 1
count += 1
score = int(grades[start:index])
sum_n = score + sum_n
average = sum_n / count
print(name_last_first, " ", average, "<---", grades)
print('---------------------------------------------------------')
grade_file.close()
I just need to figure out how to have even spaces so it makes an even row and column like the first output. Help is greatly appreciated! Thanks!
One way is to use Python's builtin C-style formatting. Note that a negative field width indicates that the field is to be left-justified:
>>> print("%-30s %4.1f" % ("Jacobson, Mark", 19.0))
Jacobson, Mark 19.0
>>>
Alternatively, you can use the string format
method:
>>> print("{:30s} {:4.1f}".format("Jacobson, Mark", 19.0))
Jacobson, Mark 19.0
>>>
You can also use Formatted String Literals (f-strings):
>>> name = "Jacobson, Mark"
>>> average = 19.0
>>> print(f"{name:30s} {average:4.1f}")
Jacobson, Mark 19.0
>>>
这篇关于Python:均匀间隔具有不同字符串长度的输出数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!