如何打印 Python 中特定月份范围内的事务日志

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

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

如何根据月份打印特定范围的日志? 想象一下,如果我想每季度或每半年打印一次日志,而我的电脑本地时间是十一月。

我希望 python 打印出 9 月到 11 月的所有日志,因为我想根据本地时间每季度打印一次日志。

编辑:

以下是我的尝试,但还是达不到我的预期

# ↓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()

这是一种处理日志的简单方法。

让我们导入您在 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.

让我们天真地解析原始日志行(缩放时可能效率不高):

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")

设置好框架后,按季度编制索引非常简单:

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

returns 预期行:

                          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

如果您必须解析大量日志,那么您可能需要提高这个简单解决方案的性能。无论如何,将日志格式更改为众所周知的 CSV 或 FWF 格式是一个好的开始。