如何将文本从一个文件写入另一个文件并进行排序

时间:2018-12-01 01:41:27

标签: python

我必须生成另一个文本文件,并根据其关联组织对比赛结果进行分组。我的问题是,由于文件已经打开,我可以继续进行排序吗?如何根据他们不同的关联组织将它们分为几类?

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()

0 个答案:

没有答案