jeudi 24 octobre 2019

How to loop over .dat file and append specific column per set of lines to an array

In the .dat file, the first line [it’s an integer, I called S) indicates the number of sets present but also the number of variations per set. For example, if S is 21, there are 21 sets present of each 21 lines.

Then, as from the next line there are 6 columns. I want to append the 4th column per set of S lines to an array to make some calculations later on. I want to stop doing that till line S*S. The complication is that I need to ignore (don’t append) every 21st line per set since they’re fictitious results.

I’m not completely sure how to go about it, but here is what I got for now. Grateful for any help.

with open('foo.dat') as f:

    S = int(f.readline())  #extracting set number
    print("Sets, S:", S)

    I   = [] 

    for i, lines in enumerate(f):
        columns = lines.split()

        if i < S*S: 

            In = float(columns[3])                                                            
            I.append(In)   #extracting 4th column

    #checking results        
    print(I)
    print("length of I:", len(I))

Please find attached link to an example .dat file i’m working with: https://pastebin.com/Cgms3efh

Aucun commentaire:

Enregistrer un commentaire