使用RE让Python在某个时间停止从文件中打印行

发布于 2025-01-19 08:22:14 字数 1017 浏览 3 评论 0原文

基本上,我要做的是让它打印在用户输入的特定范围内具有时间戳的文件行。

import sys, re

f = open('log.txt', encoding='ISO-8859-1')
h = open('errorsfound.txt', "w")
print("What time do you want to begin searching the logs?")
starttimestamp = input()
starttimestamp = str(starttimestamp)
print("What time do you want to stop searching the logs?")
endtimestamp = input()
endtimestamp = str(endtimestamp)
use = False
lines = f.readlines()
for line in lines:

  if re.search(starttimestamp, line): use = True
  if re.search(endtimestamp, line): use = False
  if use:
      if "error" in line:
          h.write (line)
  if use:
      if "Failed" in line:
          h.write (line)
  if use:
      if "ERROR" in line:
          h.write (line)
  if use:
      if "WARN" in line:
          h.write (line)
  if use:
      if "Requeue:" in line:
          h.write (line)


f.close()
h.close()

从一定时间开始,我可以将其打印到印刷品,但是在一定时间后我很难停止打印。时间戳为格式04:00:48。因此,当它提示时,我输入说04:00:48作为开始时间,而04:15:48作为结束时间。我是一个相当新的编码员,所以如果我遗漏了任何东西,我深表歉意。谢谢您的查看!

Basically what I am trying to do is to get it to print the lines of a file that have a timestamp in a specific range that is input from the user.

import sys, re

f = open('log.txt', encoding='ISO-8859-1')
h = open('errorsfound.txt', "w")
print("What time do you want to begin searching the logs?")
starttimestamp = input()
starttimestamp = str(starttimestamp)
print("What time do you want to stop searching the logs?")
endtimestamp = input()
endtimestamp = str(endtimestamp)
use = False
lines = f.readlines()
for line in lines:

  if re.search(starttimestamp, line): use = True
  if re.search(endtimestamp, line): use = False
  if use:
      if "error" in line:
          h.write (line)
  if use:
      if "Failed" in line:
          h.write (line)
  if use:
      if "ERROR" in line:
          h.write (line)
  if use:
      if "WARN" in line:
          h.write (line)
  if use:
      if "Requeue:" in line:
          h.write (line)


f.close()
h.close()

I have it to where it prints starting from a certain time, but I'm having trouble getting it to stop printing after a certain time. The timestamps are in format 04:00:48. So when it prompts I input say 04:00:48 as the start time and 04:15:48 as the end time. I am a fairly new coder so I apologize if I have left anything out. Thank you for looking!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放我走吧 2025-01-26 08:22:14

作为一个新的编码器,您缺少某种格式,但无论如何工作!

在时间的时间里,您应该习惯使用诸如DateTime之类的模块,使用str()输入是多余的(如果在排队中的my_text:然后x),但是无论如何,re是一个不错的工具。

import sys, re
import datetime

f = open('log.txt', encoding='ISO-8859-1')
h = open('errorsfound.txt', "w")
print("What time do you want to begin searching the logs?")
starttimestamp = str(input()) # redundant use of str()
print("What time do you want to stop searching the logs?")
endtimestamp = str(input())   # redundant use of str()
# use = False # bad name for a variable, use more descripting ones
write_time_flag = False
lines = f.readlines()

"""
    my_solution
"""

try:
    start_time_stamp = datetime.datetime.strptime(input, "%H:%M:%S")
    break
except:
    print("Use Format Hours:Minutes:seoconds (HH:MM:SS)")

try:
    end_time_stamp = datetime.datetime.strptime(input, "%H:%M:%S")
    break
except:
    print("Use Format Hours:Minutes:seoconds (HH:MM:SS)")

for line in lines:

  # I don't know the format of your log file so, read the hours with datetime
  # and check condition

  if readtime > start_time_stamp and readtime < end_time_stamp:
      use = True
  else:
      use = False

  if use:
      if "error" in line:
          h.write (line)
      elif "Failed" in line:
          h.write (line)
      elif "ERROR" in line:
          h.write (line)
      elif "WARN" in line:
          h.write (line)
      elif "Requeue:" in line:
          h.write (line)

f.close()
h.close()

As a new coder you are missing some format but good job anyways!

For time you should get used to use modules like datetime, using str() on input is redundant since it saves the result of the input as str already, and least but not last re is for regular expressions, there are other ways of parsing text ( if my_text in line: then x) but re is a good tool anyways.

import sys, re
import datetime

f = open('log.txt', encoding='ISO-8859-1')
h = open('errorsfound.txt', "w")
print("What time do you want to begin searching the logs?")
starttimestamp = str(input()) # redundant use of str()
print("What time do you want to stop searching the logs?")
endtimestamp = str(input())   # redundant use of str()
# use = False # bad name for a variable, use more descripting ones
write_time_flag = False
lines = f.readlines()

"""
    my_solution
"""

try:
    start_time_stamp = datetime.datetime.strptime(input, "%H:%M:%S")
    break
except:
    print("Use Format Hours:Minutes:seoconds (HH:MM:SS)")

try:
    end_time_stamp = datetime.datetime.strptime(input, "%H:%M:%S")
    break
except:
    print("Use Format Hours:Minutes:seoconds (HH:MM:SS)")

for line in lines:

  # I don't know the format of your log file so, read the hours with datetime
  # and check condition

  if readtime > start_time_stamp and readtime < end_time_stamp:
      use = True
  else:
      use = False

  if use:
      if "error" in line:
          h.write (line)
      elif "Failed" in line:
          h.write (line)
      elif "ERROR" in line:
          h.write (line)
      elif "WARN" in line:
          h.write (line)
      elif "Requeue:" in line:
          h.write (line)

f.close()
h.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文