我有一个txt文件,其中第1-5行都是单词,第6行及以上的开头有timestamp
,如图所示:
This is a document1
This is a document2
This is a document3
This is a document4
This is a document5
2019-05-27 07:00:00, value1, value2, value3
2019-05-27 06:38:00, value1, value2, value3
2019-05-27 07:05:00, value1, value2, value3
如何将第6行排序到最早时间在上面,最晚时间在下面的最后一行?你知道吗
这是我试图基于另一个堆栈溢出问题,但没有工作。你知道吗
lines = sorted(open(outputFile.txt).readlines(), key=lambda line: line[5:-1].split(",")[0])
outFile.close()
如果您不“需要”单行线,可以执行以下操作:
未经测试,但应该有效。你知道吗
您可以将文件读取为pandas DataFrame,然后在相应的行上使用sort_values()。你知道吗
另外,我建议将列强制转换为它们的类型,并将表转换为整洁的格式->;这里第一列应该仅为datetime
使用这种方法,您基本上有两条生产线(不带铸造):
这里(in1.txt是来自post的数据)
输出
相关问题 更多 >
编程相关推荐