I made a log file, and I want to take it and want to find the last instance that the specific user was online, the log file looks like this:
----------------------------------------------
Logging started: 2019-12-05 12:42:19.591833
Num of members in Test Server: 12
Members:
test12345#0720
A part of server: Test Server
Current status: idle
Current activity: None
Logging complete
-----------------
Now this repeats multiple times because it adds a new log every X amount of time, and I want to find the last time Z user was online. (I have the name and discriminator of the user)
You can read the file line-by-line, and collect everything into a list up to a Logging complete
, then check if user was present and store the candidate in an auxiliary variable, then continue with an empty list.
At the end the auxiliary variable will contain the last log block when the user was present.
Something like this:
user="test12345#0720"
last=None
current=[]
with open("test.log") as f:
for line in f:
current.append(line.strip()) # line contains the line-break character(s) too
if line.startswith("Logging complete"): # if there may be extra info in the line
if user in current:
last=current
current=[]
if last:
print("\n".join(last))
(It collects the ------
lines too, getting rid of them is left to the reader)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.