Python:按任意列对文件排序,其中列包含时间值 [英] Python : Sort file by arbitrary column, where column contains time values

查看:177
本文介绍了Python:按任意列对文件排序,其中列包含时间值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个人的.txt文件,每个人旁边都有两次.这是.txt文件

I have a .txt file for a person, and next to each person they have two times. This is the .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

我需要阅读文件,然后获取每一行,阅读它,然后第二次对整个列表进行排序.记住在文件中数字是字符串对象.因此基本上,最早的时间,即11:00,应该与他们以前的时间和名字一起放在列表的顶部.例如. Xantippe 09:00 11:00,然后在另一行上的另一行,等等.

I am required to read the file, then get each line, read it, and sort the whole list by the second time. Remember in the file the 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 one 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 have managed to sort the list by name in ascending order, but how do I sort with the last time?

推荐答案

首先,您需要将数据转换为可用的格式...因此,让我们将其加载到内存中的列表中-重要的是要注意dict s本质上没有顺序,因此我们要使用列表.

Firstly, you need to get your data into a usable format... So let's load it into a list in memory - it's important to note that dicts do not inherently have order, so we want to use a list.

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

这将删除所有尾随的换行符,并用空格将其分隔开...因此我们最终得到:

This will remove any trailing newlines and break it up by the space character... so we end up with:

[['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']]

然后,我们可以使用list.sort方法-itemgetter是用于获取序列的第n个元素的便捷方法,因此我们有了名称,开始,结束,其中end是第二个索引(基于第一个零,即名称)

Then, we can use the .sort method of a list - itemgetter is a handy method for getting the nth element of a sequence, so we have name, start, end, where end is the 2nd index (based on zero being the first, which will be the name)

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

最后我们得到:

[['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']]

然后将其写回:

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

这篇关于Python:按任意列对文件排序,其中列包含时间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆