'Reading line-by-line textfile with for loop and get error IndexError: list index out of range

I have a tab delimited text file containing these values.

My input textfile:

    0.227996254681648   0.337028824833703   0.238163571416268   0.183009231781289   0.085746697332588   0.13412895376826
    0.247891283973758   0.335555555555556   0.272129379268419   0.187328622765857   0.085921240923626   0.128372465534807
    0.264761012183693   0.337777777777778   0.245917821271498   0.183211905363232   0.080493183753814   0.122786059549795
    0.30506091846298    0.337777777777778   0.204265153911403   0.208453197418743   0.0715575291087 0.083682658454807
    0.222748815165877   0.337028824833703   0.209714942778068   0.084252659537679   0.142013573559938   0.234672985858848

I would like to input each line of the my textfile, run a for loop over each line with some equations and output 4 values after the equation (fsolve) and append it to another text file, line-by-line.

My code:

with open("/path/inputtextfile.txt") as f:
        for line in f:
            map(float, line.strip().split())
            if 'str' in line:
                break
            BB=columns[0]
            LL=columns[1]
            FF=columns[2]
            VV=columns[3]
            GG=columns[4]
            TT=columns[5]
            x=1
            FF2=FF**2
            BB2=1-BB
            LL2=1-LL
            def f2(z):
                a=z[0]
                b=z[1]
                c=z[2]
                d=z[3]
                f=np.zeros(4)
                f[0]=x*a*((1-c)*BB+(b-d)*BB2)-VV
                f[1] =((x**2)*(a**2)*((BB2**2)*b*d-(BB**2)*c))-GG
                f[2]= x*a*(b*BB2*(1-x*d*a*BB2)-c*BB*(1-x*a*BB))-TT
                f[3]= (LL*LL2*BB*BB2*(d - b*c)**2) -FF2
                return f
            z= fsolve(f2,[1,1,1,1])
            a_file = open("path/to/new/output/textfile.txt", "a")
            np.savetxt(a_file, z, fmt='%1.10f', newline=" ")
            a_file.close()

I am not so sure, if the whole code works fine. But something seems for sure not to work with the input of my numbers as they are not floating-point values. I get this error:

 ---------------------------------------------------------------------------
    IndexError                                Traceback (most recent call last)
    <ipython-input-86-2808bd00222a> in <module>
          5             break
          6         BB=columns[0]
    ----> 7         LL=columns[1]
          8         FF=columns[2]
          9         VV=columns[3]
    
    IndexError: list index out of range


Solution 1:[1]

Why you don't use csv module for this purpose. You only need to have the file in .csv and You could use it in this way:

import csv # to import the module in your class

 lines = []

 with open("filename.csv") as f:
     csvReader = csv.reader( f, delimiter="" ) #in your case the delimeter is space between cells, if use commas you use delimiter=","
     for row in csvReader:
         lines.append(row) #or you can use your formulas here to work each line

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1