Python: sort file by arbitrary column where column contains time values

I have a .txt file for a person and next to it each one has two. This is a .txt file

 Xantippe 09:00 11:00
 Erica 10:00 12:06
 Marcia 09:30 11:45
 Elizabeth 10:15 12:10
 Angela 11:30 13:45
 Freda 12:00 14:20
 Maria 12:30 14:10

      

I need to read a file, then get each line, read it, and sort the entire list a second time. Remember that in the file, numbers are string objects. So basically the time that is the earliest, i.e. 11:00, should be at the top of the list along with their previous time and name. eg. Xantippe 09:00 11:00

and then on another line the next, etc.

So far I have done:

from Practise1 import timeCalc
with open('LadiesRace.txt', 'r') as f:
  readf = f.read();
  timeX = timeCalc()
  lis = readf.split('\n')
  with open('sortByFinishTime.txt','w') as w:
    def compare(x,y):
      if x[1] > y[1]:
        return 1
      if x[1] < y[1]:
        return -1
      return 0
    #lis.sort()
    for l in lis:
      #line = l.strip()
      slist = l.split(' ')
      print slist[2]

      

The problem is that I cannot use a dictionary, only a list. I was able to sort the list by name in ascending order, but how do I sort the last time?

+2


source to share


1 answer


First, you need your data to be in a usable format ... So load it into a list in memory - it's important to note that it dict

's essentially no order, so we want to use a list.

with open('myfile.txt') as fin:
    lines = [line.split() for line in fin]

      

This will strip out any trailing newlines and split them with a space character ... so we get:

[['Xantippe', '09:00', '11:00'], ['Erica', '10:00', '12:06'], ['Marcia', '09:30', '11:45'], ['Elizabeth', '10:15', '12:10'], ['Angela', '11:30', '13:45'], ['Freda', '12:00', '14:20'], ['Maria', '12:30', '14:10']]

      

Then we can use the method .sort

for list

- itemgetter

- a convenient method to get the nth element of the sequence, so we have a name, start, end, where end is the second index (zero based will be the first which will be the name)



from operator import itemgetter
lines.sort(key=itemgetter(2))

      

And we get:

[['Xantippe', '09:00', '11:00'], ['Marcia', '09:30', '11:45'], ['Erica', '10:00', '12:06'], ['Elizabeth', '10:15', '12:10'], ['Angela', '11:30', '13:45'], ['Maria', '12:30', '14:10'], ['Freda', '12:00', '14:20']]

      

Then write it back:

with open('output.txt', 'w') as fout:
    for el in lines:
        fout.write('{0}\n'.format(' '.join(el)))

      

+9


source







All Articles