【问题标题】:How to print transaction logs from a specific range of months in Python如何在 Python 中打印特定月份范围内的事务日志
【发布时间】:2022-01-01 16:13:34
【问题描述】:

这是我在 txt 文件 (trans.txt) 中的日志示例:

2021 年 7 月 22 日 09:35:54 撤回:RM500

2021 年 7 月 22 日 09:35:54 撤回:RM500

2021 年 8 月 22 日 09:35:54 撤回:RM500

2021 年 8 月 22 日 09:35:54 撤回:RM500

2021 年 9 月 22 日 09:35:54 撤回:RM500

2021 年 9 月 22 日 09:35:54 撤回:RM500

2021 年 9 月 22 日 09:35:54 撤回:RM500

2021 年 10 月 22 日 09:35:54 撤回:RM500

2021 年 10 月 22 日 09:35:54 撤回:RM500

2021 年 11 月 22 日 09:35:54 撤回:RM500

2021 年 11 月 22 日 09:35:54 撤回:RM500

2021 年 12 月 22 日 09:35:54 提现:RM500

2021 年 12 月 22 日 09:35:54 提现:RM500

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

我希望 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()

【问题讨论】:

  • 您的问题描述很清楚。但是到目前为止,您尝试过什么?
  • 对不起,我应该首先包括我的尝试。我已经通过在那里添加我的尝试来编辑问题。谢谢提醒

标签: python python-3.x sorting datetime logging


【解决方案1】:

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

让我们导入您在 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"])

如果在时间戳和消息之间添加分隔符或格式化日期 以固定长度格式,例如 ISO-8601 不是一个选项,那么您需要 应对额外挑战:您的数据不是固定格式 也不是 CSV 文件格式。

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

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,:]

返回预期的行:

                          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 格式是一个好的开始。

【讨论】:

  • 哦,我明白了.....现在我知道如何处理这个问题了。非常感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多