Python Keep First Occurence Of Date

Python script to keep the first occurrence of a date in chatlogs.

    prevdate = None
    with io.open(sys.argv[1], 'r', encoding="utf-8") as f:
        for line in f.readlines():
            # Keep only first occurence of dates and
            # format them as sections; '\section*{<date>}'
            # (the * removes numbering - TeX command)
            # TODO: Some lines do not start with dates
            date = line.split(" ")[0]       # first result after splitting by space!
            if prevdate != date:
                if sys.platform == 'win32':
                    print(u"\section*{%s}" % date.encode('unicode-escape'))
                else:
                    print(u"\section*{%s}" % date)

            output_line = u" ".join(line.split(" ")[1:])
            # output_line = " ".join(str(item) for item in line.split(" "))
            # output_line = u" ".join(line.split(" ")[1:])
            print(output_line.encode('utf-8'))
            prevdate = date

20201106161932 Sample whatsapp chatlog

Sample whatsapp chatlog

tags = chatlogs

18/06/17, 22:45 - Messages to this group are now secured with end-to-end encryption. Tap for more info.
25/09/16, 21:50 - Nick Fury created group "Avengers"
18/06/17, 22:45 - Nick Fury added you
18/06/17, 22:45 - Nick Fury added Hulk
18/06/17, 22:45 - Nick Fury added Thor
18/06/17, 22:45 - Nick Fury added Tony Stark
18/06/17, 22:29 - Tony Stark: Here are the details for tomorrow's picnic:
The park is located at 123 Main Street. Bring your own snacks, we will also be grilling. It is going to be very warm so dress appropriately. We should be getting there at noon. See you then and don't forget the sunscreen.
18/06/17, 22:46 - Hulk: HULK NO CARE
18/06/17, 22:46 - Hulk: HULK NO FRIEND HERE
18/06/17, 22:46 - Hulk: HULK HATE LOKI
18/06/17, 22:46 - Hulk: GFCHGK
18/06/17, 22:47 - Thor: Stop pressing every button in there
18/06/17, 22:47 - Loki: Why do you have 2 numbers, Banner?
18/06/17, 22:48 - Hulk: HULK FIRST SMASH YOU THEN TELL YOU

20201104162315 chatlogs for jamie

Related