2

i have this big txt file

G29 2019 07 08 02 00 00 1.571122556925e-04-9.777068044059e-12 0.000000000000e+00
     6.100000000000e+01 6.809375000000e+01 3.670867192067e-09 2.120402980941e+00
     3.512948751450e-06 1.192553318106e-03 9.380280971527e-06 5.153736095428e+03
     9.360000000000e+04 4.656612873077e-08 1.565794672787e+00 8.195638656616e-08
     9.854740594187e-01 2.112812500000e+02 1.817507992857e+00-7.755323040334e-09
     2.692969315838e-10 1.000000000000e+00 2.061000000000e+03 0.000000000000e+00
     2.000000000000e+00 0.000000000000e+00-9.778887033463e-09 6.100000000000e+01
     9.999000000000e+08 0.000000000000e+00                                      
G30 2019 07 08 02 00 00-1.387689262629e-04-7.503331289627e-12 0.000000000000e+00
     4.300000000000e+01 3.143750000000e+01 5.236289541049e-09-1.114281617593e+00
     1.536682248116e-06 4.139962256886e-03 5.565583705902e-06 5.153780742645e+03
     9.360000000000e+04 3.166496753693e-08-5.518871386231e-01-4.097819328308e-08
     9.406574455640e-01 2.614687500000e+02-2.937938331210e+00-8.484639133562e-09
    -4.607334771129e-11 1.000000000000e+00 2.061000000000e+03 0.000000000000e+00
     2.000000000000e+00 0.000000000000e+00 3.725290298462e-09 4.300000000000e+01
     9.999000000000e+08 0.000000000000e+00                                      
J02 2019 07 08 01 00 00-9.192153811455e-07-3.410605131648e-13 0.000000000000e+00
     9.700000000000e+01-2.437500000000e+00 1.713285650939e-09-1.895301564152e+00
     1.378357410431e-07 7.528792973608e-02-1.499429345131e-06 6.493121416092e+03
     9.000000000000e+04 4.768371582031e-07-9.331615762110e-02-2.438202500343e-06
     7.607943375436e-01 2.135937500000e+02-1.583245983476e+00-1.648282943315e-09
     7.928901699153e-11 2.000000000000e+00 2.061000000000e+03 0.000000000000e+00
     2.900000000000e+00 0.000000000000e+00 1.396983861923e-09 8.650000000000e+02
     9.999000000000e+08 0.000000000000e+00    

and i need to read all lines that start with G30 and all lines below (in this sample 7 lines) until next fragment starts. I managed to read only first line and number of this line but can't add other lines.

with open("filename", "r") as f:

    str1 = 'G30'
    for i,ln in enumerate(f):
        if  str1 in ln:
            print(ln[0:],i)

5 Answers5

1

Since you know the index of the line at which "G30" starts, just pick the next 7 lines.

with open("filename", "r") as f:
    str1 = 'G30'
    all_lines = f.readlines()
    for i,ln in enumerate(all_lines):
        if  str1 in ln:
            print(ln[0:])
            for l in range(1,8):
                print(all_lines[i+l])
Amulya
  • 58
  • 4
0

To process the file content as a stream, without reading the entire content into memory, I think your best option will be to use a flag variable to decide whether it's time to print the current line or not. Consider this algorithm:

  • If the line matches the start pattern, set the flag
  • If the line matches the end pattern, clear the flag
  • If the flag is set, print the current line

For example, like this:

with open("filename") as fh:
    pattern = 'G30'
    should_print = False

    for line in fh:
        if line.startswith(pattern):
            should_print = True
        elif not line.startswith(' '):
            should_print = False

        if should_print:
            print(line, end='')
janos
  • 120,954
  • 29
  • 226
  • 236
0

Try:

fname = "testing.txt"
with open(fname) as f: 
    lines = f.readlines()
    listlen = len(lines)
    i = 0
    while i<listlen: 
        line = lines[i]
        if line.startswith("G30"): 
            print(line, end="")
            count =0
            while True: 
                i=i+1
                line = lines[i]
                print(line, end="")
                count=count+1
                if count>=7: break
        i=i+1
rnso
  • 23,686
  • 25
  • 112
  • 234
0
while filename.readline()[:3] ==‘G30’:
    exit()

As soon as the line starts with the program stops.

0

You can create a separate function to print required number of lines:

# this fn prints n number of lines
# it also removes printed lines from list
# and return remaining list: 
def printNlines(linelist, n):
    i=0
    while i<n: 
        if len(linelist)==0: break  # return if list becomes empty
        print(linelist[0], end="")  
        linelist = linelist[1:]     # remove printed line from list;
        i=i+1
    return linelist                 # return remaining list;

Now use above function to go through your file list:

fname = "testing.txt"
with open(fname) as f: 
    lines = f.readlines()       # get list of lines from file;
while len(lines)>0:             # loop till the list is empty
    line = lines[0]             # read first line
    if line.startswith("G30"):  # test it
        lines = printNlines(lines, 1+7) # print current line and seven more; 
    else: lines = lines[1:]   # remove this line from list without printing;
rnso
  • 23,686
  • 25
  • 112
  • 234