我是python的新手,我必须按日期排序一个包含大量行的大量文件文本:
CCC!LL!EEEE!EW050034!2016-04-01T04:39:54.000Z!7!1!1!1
CCC!LL!EEEE!GH676589!2016-04-01T04:39:54.000Z!7!1!1!1
CCC!LL!EEEE!IJ6758004!2016-04-01T04:39:54.000Z!7!1!1!1
有人能帮助我吗?
谢谢大家!
答案 0 :(得分:1)
您是否考虑过使用* nix sort
计划?在原始术语中,它可能比大多数Python脚本更快。
使用-t \!
指定列由!
字符-k n
分隔以指定字段,其中n
是字段编号,-o outputfile
如果要将结果输出到新文件。
例如:
sort -t \! -k 5 -o sorted.txt input.txt
将对其第5个字段进行排序input.txt
,并将结果输出到sorted.txt
答案 1 :(得分:0)
我想将时间转换为时间戳然后排序。 首先将日期转换为列表。
rawData = '''CCC!LL!EEEE!EW050034!2016-04-01T04:39:54.000Z!7!1!1!1
CCC!LL!EEEE!GH676589!2016-04-01T04:39:54.000Z!7!1!1!1
CCC!LL!EEEE!IJ6758004!2016-04-01T04:39:54.000Z!7!1!1!1'''
a = rawData.split('\n')
>>> import dateutil.parser,time
>>> sorted(a,key= lambda line:time.mktime(dateutil.parser.parse(line.split('!')[4]).timetuple()))
['CCC!LL!EEEE!EW050034!2016-04-01T04:39:54.000Z!7!1!1!1 ', ' CCC!LL!EEEE!GH676589!2016-04-01T04:39:54.000Z!7!1!1!1', ' CCC!LL!EEEE!IJ6758004!2016-04-01T04:39:54.000Z!7!1!1!1']
答案 2 :(得分:0)
Take a look into regular expression module, I've used it a couple of times and it looks lretty simple to do what you want with this module https://docs.python.org/2/library/re.html Here is the docs but try googling for regular expression python examples to make it more clear, good luck.