pythontext-filesreadlines

Python: How to read a text file line by line corresponding to values set by user and ending when line contains certain character


I have a text file that has this format:

--------------Run number= 169 ------------
-------Log book info: 
        NaI run--> Test077
         Beam ~ 320 nAmp, run duration is 1hr
         Scalar: 977493, DAQ: 339399
eventno.    X   Y   Z   Tb  Q
12345678  0
0   59.0782 -58.7822    62.24   158 180.9
0   103.18  -43.1458    50.72   149 51
0   28.7109 -70.2263    87.84   178 124.2
0   82.8706 -63.4557    62.24   158 81.2
0   89.6407 -63.4562    59.68   156 59.3
0   31.9979 -65.5526    87.84   178 66.6
0   35.4805 -63.4559    89.12   179 84.7
0   38.7676 -58.7821    89.12   179 133.6
12345678  1
12345678  2
2   25.2279 -72.3226    48.16   147 221.55
2   28.7109 -70.2263    48.16   147 1587.7
2   76.1009 -63.4562    46.88   146 110.35
2   31.9979 -65.5526    48.16   147 1601.8
2   35.4805 -63.4559    48.16   147 310.25
2   31.9979 -58.7826    49.44   148 492.8
2   35.4805 -56.6859    46.88   146 42.6
2   1.63117 -43.1461    73.76   167 54.55
2   4.91818 -38.4723    76.32   169 75.4
2   11.6882 -38.4723    76.32   169 325.95
2   18.4578 -38.4719    72.48   166 76
2   15.1708 -43.1457    77.6    170 144.6

What I'm trying to do is start reading the file line by line corresponding to an event number (1st column of the data) that the user inputs (variable event_number) and the program stops reading the file once it reaches a certain character in the text file that indicates that the data corresponding to that event has ended: in this case it is a number set by the user (variable magic_number = 12345678). I also want the corresponding data (X,Y,Z,Tb,Q) to the event number to be stored in separate lists. My code is as follows:

event_number = '0'
magic_number = '12345678'

x = []
y = []
z = []
tb = []
q = []

file = 'Runnumber169raw10.txt'

with open(file, 'r') as raw_dat:
    for line in raw_dat:
        
        if line[0] == event_number:
            x.append(line[1])
            y.append(line[2])
            z.append(line[3])
            tb.append(line[4])
            q.append(line[5])
        
        if magic_number in line:
            break

When I run the program, I don't see any errors; but the lists don't seem to appending any values. I'm not sure if I'm missing something here or my program isn't reading the lines as I want properly. Was wondering if someone can provide any insight as to why this is the case.


Solution

  • I don't think you actually need the magic number. Each line that corresponds to an event starts with the event number on its own, so simply drop every line that doesn't start with the event number you want:

    with open(file, 'r') as raw_dat:
       records = [line.split() for line in raw_dat if line.split()[0] == event_number]
    

    Then split into your five lists:

    # split into five lists
    x  = [record[1] for record in records]
    y  = [record[2] for record in records]
    z  = [record[3] for record in records]
    tb = [record[4] for record in records]
    q  = [record[5] for record in records]
    

    If it's a very large data set if might be simpler to use pandas, but that might be overkill for a simple problem.