Nov-04-2020, 04:51 PM
I am new to python and got a requirement to sort the content of the text file based on timestamp in reverse order. Below is the content of text file(in.txt)
2020/10/31:09:05:01 734691 445750 384860 557946
2020/10/31:15:05:01 734691 366500 315620 554140
2020/10/31:21:05:01 705959 177500 153041 513408
Below was written but getting the below error.
key=lambda line: datetime.strptime(line.split(" ")[0], "%Y/%m/%d:%H:%M:%S"),reverse=True)
File "/usr/lib64/python2.7/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data '' does not match format '%Y/%m/%d:%H:%M:%S'
Unable to determine why the error is happening.Could please help.
2020/10/31:09:05:01 734691 445750 384860 557946
2020/10/31:15:05:01 734691 366500 315620 554140
2020/10/31:21:05:01 705959 177500 153041 513408
Below was written but getting the below error.
from datetime import datetime with open('in.txt') as f: sorted_lines = sorted([l.rstrip() for l in f.readlines()], key=lambda line: datetime.strptime(line.split(" ")[0], "%Y/%m/%d:%H:%M:%S")) "%Y/%m/%d:%H:%M:%S"),reverse=True) for line in sorted_lines: print(line)Error
key=lambda line: datetime.strptime(line.split(" ")[0], "%Y/%m/%d:%H:%M:%S"),reverse=True)
File "/usr/lib64/python2.7/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data '' does not match format '%Y/%m/%d:%H:%M:%S'
Unable to determine why the error is happening.Could please help.