[SOLVED] How to loop past metadata at the beginning of a data file?

Issue

I have got a data file, with data at the top and then 2 columns of data. What I am trying to do is loop past the data and then split the 2 columns into separate arrays, so I can plot them.

Solution

Your intention seems to be to loop through the file until you hit "END_OF_METADATA". That’s the right idea. But you close the file after you’ve reached that line and then let np.genfromtext open it again.

Try:

with open("filedata.txt", "r") as file:
    for line in file:
        if line.strip() == "END_OF_METADATA":
            break
    data = np.genfromtxt(file, skip_header=1)

Output (print(data)):

array([[0.00000000e+00, 3.52491663e-03],
       [1.59154943e+03, 3.54231419e-03],
       [3.18309886e+03, 3.55984087e-03],
       [4.77464829e+03, 3.57749795e-03],
       [6.36619772e+03, 3.59528672e-03],
       [7.95774715e+03, 3.61320850e-03],
       [9.54929658e+03, 3.63126461e-03],
       [1.11408460e+04, 3.64945640e-03],
       [1.27323954e+04, 3.66778524e-03],
       [1.43239449e+04, 3.68625250e-03],
       [1.59154943e+04, 3.70485958e-03],
       [1.75070437e+04, 3.72360790e-03],
       [1.90985932e+04, 3.74249889e-03],
       [2.06901426e+04, 3.76153400e-03],
       [2.22816920e+04, 3.78071470e-03]])

Answered By – Timus

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *