我必须生成另一个文本文件,并根据其关联组织对比赛结果进行分组。我的问题是,由于文件已经打开,我可以继续进行排序吗?如何根据他们不同的关联组织将它们分为几类?
def get_sec(time_str):
h, m = time_str.split(':')
return int(h) * 3600 + int(m) * 60 # CHANGES the minutes to seconds
with open("~/Desktop/Race_Results_Sample.txt", "r")as myList:
myList = myList.read() # reads from list
myList = [l.split(",") for l in myList.splitlines()]
myList = sorted(myList, key=lambda kv: kv[1])
for line in myList:
num, last, org, time = line
place = []
place.append(time)
placenum = enumerate(sorted(place))
print(place, placenum)
for rank, value in enumerate(sorted(place)):
print(rank, value)
for line in myList:
num, last, org, time = line
new_time = get_sec(time)
mile = round((((new_time/ 3.10686)/60)/60), 3)
mile = str(mile)
print ('{:<20s}{:<5s}{:<5s}{:<7s}{:<10s}'.format(last, num, org, time, mile))
lines = myList.readlines()