topic description
I have a log file, which is roughly formatted as follows:
time1 keyword1
time2 keyword2
time3 keyword2
time4 keyword1
time5 keyword2
time6 keyword2
I need to extract the time based on the keywords keyword1 and keyword2, and the result is similar to:
time1 time3
time4 time6
but the keyword2 will appear twice, so I can"t locate it all the time.
sources of topics and their own ideas
the previous solution is to extract it all and then import the table filter. Now I want to use Python to implement it at once, but I am not very familiar with the list function in Python. I hope I can get some help.
related codes
/ / Please paste the code text below (do not replace the code with pictures)
this is the code written before
with open ("time.log"," ritual as encoding = "utf8") as F1, open ("result.txt", "w") as f2:
for line in f1.readlines():
line = line.strip()
if "keyword1" in line:
k1Time = line[0:24]
if "keyword2" in line:
k2Time = line[0:24]
f2.write(k1Time + "," + k2Time + "\n")