简体   繁体   中英

How do i sort line by date in python?

I have this temp.txt file:

73.00   1   87  241531  02/06/19    1
95.00   1   87  244012  06/06/19    1
47.00   1   87  447     08/01/13    0
126.00  1   87  242697  08/06/19    1
106.00  1   87  242699  08/06/19    1
94.00   1   87  242293  09/06/19    1
192.00  1   87  242710  09/06/19    1
54.00   1   87  243243  13/06/19    1
106.00  1   87  243421  13/06/19    1

And I want to sort the lines by their dates.

Here is my code (does not work):

from datetime import datetime
import re


def func():

    temp_file = open("./temp.txt", "w")
    timestamp_regex = re.compile("[^:]+:\d\d:\d\d")

    def convert_time(logline):
        stamp = timestamp_regex.match(logline).group()  # this will error if there's no match.
        d = datetime.strptime(stamp, "%d/%m/%Y")
        return int(d.timestamp())

    temp_file_sorted_lines = sorted(open("./temp.txt", "r").readlines(), key=lambda line: line.split()[4])

    temp_file.close()

What am I doing wrong? how do I sort the lines correctly?

This is one approach using datetime and sorted

Ex:

import datetime

with open(filename) as infile:
    data = sorted([line.split() for line in infile], key=lambda x: datetime.datetime.strptime(x[-2], "%d/%m/%y"))

print(data)

Output:

[['47.00', '1', '87', '447', '08/01/13', '0'],
 ['73.00', '1', '87', '241531', '02/06/19', '1'],
 ['95.00', '1', '87', '244012', '06/06/19', '1'],
 ['126.00', '1', '87', '242697', '08/06/19', '1'],
 ['106.00', '1', '87', '242699', '08/06/19', '1'],
 ['94.00', '1', '87', '242293', '09/06/19', '1'],
 ['192.00', '1', '87', '242710', '09/06/19', '1'],
 ['54.00', '1', '87', '243243', '13/06/19', '1'],
 ['106.00', '1', '87', '243421', '13/06/19', '1']]

You could use pandas:

import pandas as pd
df=pd.read_csv('file.txt',header=None, sep='\s+')
df['tmp'] = pd.to_datetime(df[4])
df = df.sort_values('tmp')
df.drop('tmp',axis=1,inplace=True)
df.to_csv('file_new.txt',header=False, index=False,sep='\t')

Output in new file:

47.0    1   87  447 08/01/13    0
73.0    1   87  241531  02/06/19    1
95.0    1   87  244012  06/06/19    1
54.0    1   87  243243  13/06/19    1
106.0   1   87  243421  13/06/19    1
126.0   1   87  242697  08/06/19    1
106.0   1   87  242699  08/06/19    1
94.0    1   87  242293  09/06/19    1
192.0   1   87  242710  09/06/19    1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM