Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python sort date
#3
from datetime import datetime


def parse_datetime(line):
    date_str, _ = line.split(maxsplit=1)
    date_fmt = "%Y/%m/%d:%H:%M:%S"
    return datetime.strptime(date_str, date_fmt)


with open("in.txt") as fd:
    for line in sorted(fd, key=parse_datetime, reverse=True):
        # line is still a str
        print(line.strip())
        # print(line, end="")
This will still fail with corrupt data.
You can catch these errors and return datetime.min, which is the earliest possible date represented by datetime.


def parse_datetime(line):
    date_fmt = "%Y/%m/%d:%H:%M:%S"
    try:
        date_str, _ = line.split(maxsplit=1)
        return datetime.strptime(date_str, date_fmt)
    except ValueError:
        # invalid format
        return datetime.min
        # datetime.min == datetime.datetime(1, 1, 1, 0, 0)
        # used as minimum value
        # you can't mix if you sort, so all elements must be a datetime
  • fd (the file object) is an iterator. Iterating over the fd will split the lines.
  • sort(fd) will sort all lines of the whole file in lexicographical order.
  • The key function of sort return a datetime object.
  • sorting requires comparison. Python has a strong TypeSafety, so you can't for example compare an int with a str. But you can ship around with the key function, which always return the same type.

    If you want to put the data into a data-structure (e.g. a dict or namedtuple), I would do the parsing first, put this data into a list and sort the list, when everything has been finished.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
python sort date - by beginner2020 - Nov-04-2020, 04:51 PM
RE: python sort date - by buran - Nov-04-2020, 05:16 PM
RE: python sort date - by DeaD_EyE - Nov-04-2020, 05:18 PM
RE: python sort date - by beginner2020 - Nov-04-2020, 05:52 PM
RE: python sort date - by DeaD_EyE - Nov-04-2020, 06:02 PM
RE: python sort date - by beginner2020 - Nov-04-2020, 06:28 PM
RE: python sort date - by DeaD_EyE - Nov-05-2020, 01:14 PM
RE: python sort date - by deanhystad - Nov-04-2020, 05:23 PM
RE: python sort date - by DeaD_EyE - Nov-04-2020, 05:54 PM
RE: python sort date - by chrischarley - Nov-04-2020, 05:23 PM
RE: python sort date - by deanhystad - Nov-04-2020, 05:30 PM
RE: python sort date - by chrischarley - Nov-04-2020, 05:52 PM
RE: python sort date - by buran - Nov-04-2020, 06:33 PM
RE: python sort date - by beginner2020 - Nov-06-2020, 03:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 2,349 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 3,033 Jan-20-2024, 04:45 AM
Last Post: 1418
  How to see the date of installation of python modules. newbieAuggie2019 4 5,084 Mar-31-2023, 12:40 PM
Last Post: newbieAuggie2019
Photo a.sort() == b.sort() all the time 3lnyn0 1 2,119 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  Date format and past date check function Turtle 5 11,033 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  How to sort values descending from a row in a dataframe using python sankarachari 1 2,088 Aug-16-2021, 08:55 AM
Last Post: jamesaarr
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,962 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  How to add date and years(integer) to get a date NG0824 4 4,112 Sep-03-2020, 02:25 PM
Last Post: NG0824
  What's the best way to sort this data with Python? xtrax 2 2,395 Mar-15-2020, 08:08 AM
Last Post: Larz60+
  Date from from excel to Python. atp_13 1 2,479 Nov-24-2019, 12:26 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020