Search code examples
pythonreadlinesstartswith

Selecting line from file by using "startswith" and "next" commands


I have a file from which I want to create a list ("timestep") from the numbers which appear after each line "ITEM: TIMESTEP" so:

timestep = [253400, 253500, .. etc]

Here is the sample of the file I have:

ITEM: TIMESTEP
253400
ITEM: NUMBER OF ATOMS
378
ITEM: BOX BOUNDS pp pp pp
-2.6943709180241954e-01 5.6240920636804063e+01
-2.8194230631882372e-01 5.8851195163321044e+01
-2.7398090193568775e-01 5.7189372326936599e+01
ITEM: ATOMS id type q x y z 
16865 3 0 28.8028 1.81293 26.876 
16866 2 0 27.6753 2.22199 27.8362 
16867 2 0 26.8715 1.04115 28.4178 
16868 2 0 25.7503 1.42602 29.4002 
16869 2 0 24.8716 0.25569 29.8897 
16870 3 0 23.7129 0.593415 30.8357 
16871 3 0 11.9253 -0.270359 31.7252 
ITEM: TIMESTEP
253500
ITEM: NUMBER OF ATOMS
378
ITEM: BOX BOUNDS pp pp pp
-2.6943709180241954e-01 5.6240920636804063e+01
-2.8194230631882372e-01 5.8851195163321044e+01
-2.7398090193568775e-01 5.7189372326936599e+01
ITEM: ATOMS id type q x y z 
16865 3 0 28.8028 1.81293 26.876 
16866 2 0 27.6753 2.22199 27.8362 
16867 2 0 26.8715 1.04115 28.4178 
16868 2 0 25.7503 1.42602 29.4002 
16869 2 0 24.8716 0.25569 29.8897 
16870 3 0 23.7129 0.593415 30.8357 
16871 3 0 11.9253 -0.270359 31.7252

To do this I tried to use "startswith" and "next" commands at once and it didn't work. Is there other way to do it? I send also the code I'm trying to use for that:


timestep = []
with open(file, 'r') as f:
    lines = f.readlines()
    for line in lines:
        line = line.split()
        if line[0].startswith("ITEM: TIMESTEP"):
            timestep.append(next(line))     
            print(timestep)



Solution

  • The logic is to decide whether to append the current line to timestep or not. So, what you need is a variable which tells you append the current line when that variable is TRUE.

    timestep = []
    append_to_list = False # decision variable
    
    with open(file, 'r') as f:
        lines = f.readlines()
        for line in lines:
            line = line.strip() # remove "\n" from line
            if line.startswith("ITEM"):
                # Update add_to_list
                if line == 'ITEM: TIMESTEP':
                    append_to_list = True
                else:
                    append_to_list = False
            else:
                # append to list if line doesn't start with "ITEM" and append_to_list is TRUE
                if append_to_list:
                    timestep.append(line)
    print(timestep)
    

    output:

    ['253400', '253500']