for line in lines:
#name is what there is before the first comma
name = line[:line.find(",")]
#marks are what there is after the second comma and are comma separated
marks = line[line.find(",")+1:].split(",")
#sort the marks
marks = sorted(marks,key=int)
#if you want to print only the highest
print "%s,%s"%(name,marks[-1])
p = "/home/vivek/Desktop/test_input.txt"
result = {}
with open(p, "rb") as fp:
for i in fp.readlines():
tmp = i.split(",")
try:
result[(tmp[0])] = eval(tmp[1]) + eval(tmp[2]) + eval(tmp[3])
except:
pass
alphabetical_name = result.keys()
alphabetical_name.sort()
for i in alphabetical_name:
print "Name:%s, Highest score: %d"%(i, result[i])
所以我首先要隔离所有的线:
假设我将继续以下内容:
^{pr2}$然后我将首先按名称对行进行排序
然后,对于要隔离标记的每一行,将其排序并打印回原处:
readlines()
方法从文件中读取行。在Name
是Key
,Value
是总分。在keys
。在sort()
方法按字母表对列表排序。在代码
输出:
^{pr2}$相关问题 更多 >
编程相关推荐