I want to know what the most efficient way is to parse a text file. For example, lets say I have the following text file:
Number of connections server is: 1
Server status is: ACTIVE
Number of connections to server is: 4
Server status is: ACTIVE
Server is not responding: 13:25:03
Server connection is established: 13:27:05
What I want to do is to go through the file and gather information. For example, number of connections to the server, or the times the server went down. I want to save these values in maybe lists, so that I can view or plot them later.
So what is the best way to perform this, assuming I have my keywords in a list as follows:
referenceLines = ['connections server', 'Server status', 'not responding']
Note that I do not have the complete sentence in the list, but only a part of it. I want to go through the file, line-by-line, and check if the read line corresponds to any entry in the referenceLines list, if so, get the index of the list entry and call the corresponding function.
What would be the most efficient (time, memory) way to do this, as a typical text file will be about 50MB in size.
Thank you.
Any
for line in open('filename.txt', 'r'): --do whatever??