Search code examples
pythonnumpytxt

Reading a text file and calling each array in Python


I am reading a .txt file using f.read. But I also want to assign it as a list and call each array within this list . I present the current and expected output.

f = open("Test.txt", "r")
print("f read =",f.read()) 
print("f 0 =",f[0])

The current output is

f read = [array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
         4.02753575e+002,  3.79606698e+002,  0.00000000e+000,
         3.01629825e+002,  2.73930748e+002,  2.73930748e+002,
         1.44270041e-014, -1.19840421e-014,  2.48466223e+002,
         4.07808390e-014,  2.23084563e+002,  2.23084563e+002,
         1.95133833e+002, -1.90524280e-014,  1.13675881e+002,
         1.60355972e-023,  8.87020057e+001,  6.51230922e+001,
         8.87020057e+001,  1.00000000e-100])
 array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
         3.96370317e+002,  3.82498020e+002,  0.00000000e+000,
         3.77200451e+002,  3.22917850e+002,  3.49391875e+002,
         3.24649000e+002,  3.06639099e+002,  3.06639099e+002,
         3.06639099e+002,  2.74439993e+002,  3.06639099e+002,
         2.42357202e+002,  2.42357202e+002,  2.26139235e+002,
         3.06639099e+002,  2.22347429e+002,  1.64667856e+002,
        -1.26698275e-013,  1.69645075e+002,  1.07059051e+002,
         1.95176276e+002,  1.00000000e-100])                 ]


Traceback (most recent call last):

  in <module>
    print("f 0 =",f[0])

TypeError: '_io.TextIOWrapper' object is not subscriptable

The expected output is

f read = [array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
         4.02753575e+002,  3.79606698e+002,  0.00000000e+000,
         3.01629825e+002,  2.73930748e+002,  2.73930748e+002,
         1.44270041e-014, -1.19840421e-014,  2.48466223e+002,
         4.07808390e-014,  2.23084563e+002,  2.23084563e+002,
         1.95133833e+002, -1.90524280e-014,  1.13675881e+002,
         1.60355972e-023,  8.87020057e+001,  6.51230922e+001,
         8.87020057e+001,  1.00000000e-100])
 array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
         3.96370317e+002,  3.82498020e+002,  0.00000000e+000,
         3.77200451e+002,  3.22917850e+002,  3.49391875e+002,
         3.24649000e+002,  3.06639099e+002,  3.06639099e+002,
         3.06639099e+002,  2.74439993e+002,  3.06639099e+002,
         2.42357202e+002,  2.42357202e+002,  2.26139235e+002,
         3.06639099e+002,  2.22347429e+002,  1.64667856e+002,
        -1.26698275e-013,  1.69645075e+002,  1.07059051e+002,
         1.95176276e+002,  1.00000000e-100])                 ]

f 0 = array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
         4.02753575e+002,  3.79606698e+002,  0.00000000e+000,
         3.01629825e+002,  2.73930748e+002,  2.73930748e+002,
         1.44270041e-014, -1.19840421e-014,  2.48466223e+002,
         4.07808390e-014,  2.23084563e+002,  2.23084563e+002,
         1.95133833e+002, -1.90524280e-014,  1.13675881e+002,
         1.60355972e-023,  8.87020057e+001,  6.51230922e+001,
         8.87020057e+001,  1.00000000e-100])

Solution

  • Assuming your Test.txt file contains:

    [array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
             4.02753575e+002,  3.79606698e+002,  0.00000000e+000,
             3.01629825e+002,  2.73930748e+002,  2.73930748e+002,
             1.44270041e-014, -1.19840421e-014,  2.48466223e+002,
             4.07808390e-014,  2.23084563e+002,  2.23084563e+002,
             1.95133833e+002, -1.90524280e-014,  1.13675881e+002,
             1.60355972e-023,  8.87020057e+001,  6.51230922e+001,
             8.87020057e+001,  1.00000000e-100])
     array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
             3.96370317e+002,  3.82498020e+002,  0.00000000e+000,
             3.77200451e+002,  3.22917850e+002,  3.49391875e+002,
             3.24649000e+002,  3.06639099e+002,  3.06639099e+002,
             3.06639099e+002,  2.74439993e+002,  3.06639099e+002,
             2.42357202e+002,  2.42357202e+002,  2.26139235e+002,
             3.06639099e+002,  2.22347429e+002,  1.64667856e+002,
            -1.26698275e-013,  1.69645075e+002,  1.07059051e+002,
             1.95176276e+002,  1.00000000e-100])                 ]
    

    then, assuming the arrays in question are NumPy arrays, you can do:

    import numpy as np
    
    # read in the whole file
    with open("Test.txt", "r") as f:
        inputdata = f.read()
    
    # parse the data with eval
    data = eval(
        inputdata
        .strip()  # remove trailing whitespace
        .replace("\n", "")  # replace new lines with empty strings
        .replace(")", "),"),  # stick a comma between the arrays
        {"array": np.array}  # make sure it knows to parse array as a NumPy array
    )
    
    print(data[0])
    
    array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
            4.02753575e+002,  3.79606698e+002,  0.00000000e+000,
            3.01629825e+002,  2.73930748e+002,  2.73930748e+002,
            1.44270041e-014, -1.19840421e-014,  2.48466223e+002,
            4.07808390e-014,  2.23084563e+002,  2.23084563e+002,
            1.95133833e+002, -1.90524280e-014,  1.13675881e+002,
            1.60355972e-023,  8.87020057e+001,  6.51230922e+001,
            8.87020057e+001,  1.00000000e-100])
    

    Note that the eval function can execute arbitrary code, so only use it if you trust the source of your input file.