Home > Net >  How to print transaction logs from a specific range of months in Python
How to print transaction logs from a specific range of months in Python

Time:12-01

Here's an example of my logs in a txt file (trans.txt):

22 July 2021 09:35:54 Withdrawn: RM500

22 July 2021 09:35:54 Withdrawn: RM500

22 August 2021 09:35:54 Withdrawn: RM500

22 August 2021 09:35:54 Withdrawn: RM500

22 September 2021 09:35:54 Withdrawn: RM500

22 September 2021 09:35:54 Withdrawn: RM500

22 September 2021 09:35:54 Withdrawn: RM500

22 October 2021 09:35:54 Withdrawn: RM500

22 October 2021 09:35:54 Withdrawn: RM500

22 November 2021 09:35:54 Withdrawn: RM500

22 November 2021 09:35:54 Withdrawn: RM500

22 December 2021 09:35:54 Withdrawn: RM500

22 December 2021 09:35:54 Withdrawn: RM500

how to print a specific range of logs based on months? Imagine if i wanna print logs quarterly or half-yearly, and my pc local time is November.

I'm expecting python to print out all logs from September to November, since i want to print logs quarterly based on my local time.

EDIT:

Below are my attempt, but still can't achieve what i intended

# ↓Pulls out local time's from user pc
local_timeMonth = time.strftime("%B", obj)

# ↓Opens user's transaction logs and put them in a list
hand1 = open("trans.txt", "r")
list1 = hand1.read().splitlines()
hand1.close()

# ↓Creates a another file to store all logs with the month that is 
# intended to be printed and excludes months that are not relevant,
# but all it does is store logs from November back until January 
#it excludes December though (Pc local time is November)

for i in range(0, len(list1)):
    if local_timeMonth in list1[i]:
        test = "\n".join(list1[i::-1])
        hand = open("tempLogs.txt", "w")
        hand.write(test)
        hand.close()

        # ↓Place logs only from 3 months into list
        f = open("tempLogs.txt", "r")
        line_numbers = [0, 1, 2]
        lines = []
        # ↓Puts specific month's of log in to another list
        for i, line in enumerate(f):
            if i in line_numbers:
                lines.append(line.strip())
            elif i > 2:
                break
        # ↓Print list out into readable format
        for i in lines:
            print(i)
        f.close()

CodePudding user response:

Here is a naive way to process your logs.

Let's import data you provided in your MCVE:

import io
import pandas as pd

text = io.StringIO("""22 July 2021 09:35:54 Withdrawn: RM500
22 July 2021 09:35:54 Withdrawn: RM500
22 August 2021 09:35:54 Withdrawn: RM500
22 August 2021 09:35:54 Withdrawn: RM500
22 September 2021 09:35:54 Withdrawn: RM500
22 September 2021 09:35:54 Withdrawn: RM500
22 September 2021 09:35:54 Withdrawn: RM500
22 October 2021 09:35:54 Withdrawn: RM500
22 October 2021 09:35:54 Withdrawn: RM500
22 November 2021 09:35:54 Withdrawn: RM500
22 November 2021 09:35:54 Withdrawn: RM500
22 December 2021 09:35:54 Withdrawn: RM500
22 December 2021 09:35:54 Withdrawn: RM500""")
frame = pd.read_csv(text, header=None, names=["raw"])

If adding a separator between timestamp and message or formatting date in a fixed length format such ISO-8601 is not an option then you need to cope with an extra challenge: your data is not a Fixed With Format nor a CSV file format.

Let's parse raw log lines naively (probably not efficient when scaling):

raw = frame.pop("raw")
frame["timestamp"] = raw.apply(lambda x: pd.to_datetime(" ".join(x.split(" ")[:4])))
frame["type"] = raw.apply(lambda x: x.split(" ")[4].replace(":", ""))
frame["message"] = raw.apply(lambda x: " ".join(x.split(" ")[5:]))
frame = frame.set_index("timestamp")

Once your frame is setup, indexing quarterly is as simple as:

t0 = pd.Timestamp.now().round("1D")
q1 = t0 - pd.offsets.QuarterBegin(n=1)
q2 = t0   pd.offsets.QuarterEnd(n=0)
frame.loc[q1:q2,:]

Which returns expected lines:

                          type message
timestamp                             
2021-09-22 09:35:54  Withdrawn   RM500
2021-09-22 09:35:54  Withdrawn   RM500
2021-09-22 09:35:54  Withdrawn   RM500
2021-10-22 09:35:54  Withdrawn   RM500
2021-10-22 09:35:54  Withdrawn   RM500
2021-11-22 09:35:54  Withdrawn   RM500
2021-11-22 09:35:54  Withdrawn   RM500
2021-12-22 09:35:54  Withdrawn   RM500
2021-12-22 09:35:54  Withdrawn   RM500

If you have to parse high volume of logs then you probably will need to improve performance of this naive solution. A good start would be in anyway change log format into a well known format either CSV or FWF.

  • Related