Search code examples
pythonget

Python, Catch value in text file


I have a file (output of fluiddynamic code) in which I want to get the value written just after a line that start with dilute dimensional Zimm longest

here is reported the interested lines:

 dilute dimensional Zimm longest relaxation time, dil_chtime=
    3.29486769328041

so far I just wrote this line : but I don't know why It doesn't catch the value 3.2948676932....

 zimm = 0.
  with open('memo.dat','r') as f:
        for line in f.readlines() :
            if(line.startswith(' dilute dimensional Zimm longest')):
                print (line)
                zimm = f.readline() # I suppose that this read the next line
             else:
                pass

the output of this frame is : dilute dimensional Zimm longest relaxation time, dil_chtime= how can I get the value ?


Solution

  • You can use next(f) to fetch the next line

    Ex:

    with open('memo.dat') as f:
        for line in f:     #Iterate Each Line
            if line.strip().startswith('dilute dimensional Zimm longest'): #Check Condition
                print(next(f))     #Get Value
                break