I process log files with python. Let´s say that I have a log file that contains a line which is START
and a line that is END
, like below:
START
one line
two line
...
n line
END
What I do want is to be able to store the content between the START
and END
lines for further processing.
I do the following in Python:
with open (file) as name_of_file:
for line in name_of_file:
if 'START' in line: # We found the start_delimiter
print(line)
found_start = True
for line in name_of_file: # We now read until the end delimiter
if 'END' in line: # We exit here as we have the info
found_end=True
break
else:
if not (line.isspace()): # We do not want to add to the data empty strings, so we ensure the line is not empty
data.append(line.replace(',','').strip().split()) # We store information in a list called data we do not want ','' or spaces
if(found_start and found_end):
relevant_data=data
And then I process the relevant_data
.
Looks to far complicated for the purity of Python, and hence my question: is there a more Pythonic way of doing this?
Thanks!