This is related to a project I am working on outside of the Codecademy curriculum but I thought I would ask for advice here.
I have a long log file, in which I am seeking for the specific word EQUILIBRIUM. If the word does appear in the file, it will appear in the second position of a line that has been split by spaces. Once I find that word, I want to read atom coordinates after that word.
Is this the best way to do this? Any advice is appreciated.
class Conformer: def __init__(self,fname): self.fname = fname with open(self.fname,"r") as infile: infile_contents = infile.read() split_file = infile_contents.split('\n') eq_idx = 0 eq_found = False while(not eq_found): try: split_line = split_file[eq_idx].split() except IndexError: print("Equilibrium coordinates not found!") break try: if (split_line == "EQUILIBRIUM"): eq_found = True else: eq_idx = eq_idx + 1 except IndexError: eq_idx = eq_idx + 1 print(eq_idx)
Right now I am just printing the index of where the equilibrium statement was found, but eventually I will be reading in data from the file at this point.
The first try-except statement is if in case I do not find the word EQUILIBRIUM before EOF is reached. The second try-except statement is if in case I try to test for the presence of the word EQUILIBRIUM in a line that has fewer than 2 items (i.e., a blank line).
Is this the best way to approach this? Or is there a better way?
Thanks in advance.