Search code examples
pythonstringtext-filesdata-conversion

convert values in text file to array in python


I have a text file data.txt which looks as such:

ADMAS      8.046E+03 8.206E-03 1.532E+04 1.066E-01 6.982E+06-2.820E+00 \n
ADMAS     -6.868E-03 2.009E+05 1.454E-02 9.516E+05-1.209E+00 6.058E+06 \n 
ADMAS      1.543E+04 9.179E-01 1.459E+06 5.463E+00 3.918E+07-2.904E+01 \n 
ADMAS     -2.267E-01 9.537E+05 3.902E+00 3.071E+07-1.344E+02 1.073E+08 \n 
ADMAS      7.005E+06 2.260E+01 3.978E+07 6.296E+01 7.586E+09-2.125E+03 \n 
ADMAS      1.093E+00 6.052E+06-6.178E+00 1.065E+08-1.416E+03 1.941E+09 \n 
FAMP       3.824E+03 7.120E-02 1.848E+05 7.317E-01 5.406E+06 4.096E+00 \n 
FEPS       9.039E+01 3.571E+02 2.838E+00 3.580E+02 4.098E+01 1.892E+02 \n 

(etc. In a recurring pattern). I want to have only the ADMAS values and put them in a 6x6 array. I tried the following:

filename = "data.txt"
string_fnd_1 = "ADMAS"
textfile = open(filename, 'r')
file_lines = textfile.readlines()
textfile.close()
matches_admas = [line for line in file_lines if string_fnd_1 in line]

I get the following:

['ADMAS      8.046E+03 8.206E-03 1.532E+04 1.066E-01 6.982E+06-2.820E+00\n', 'ADMAS     -6.868E-03 2.009E+05 1.454E-02 9.516E+05-1.209E+00 6.058E+06\n',....]

Now, I want to get rid of the string "ADMAS", convert the values to float and reshape into a 6x6 array. Does someone know how to do this? Help would be appreciated.


Solution

  • According to your requirements, I came up with the following solution:

    import re
    filename = "data.txt"
    string_fnd_1 = "ADMAS"
    
    matrix = [] # 6x6 matrix of dtype float
    with open(filename, "r") as f:
        for line in f:
            if string_fnd_1 in line:
                # cleaning the bad chars in line
                line = line.strip()
                line = line.strip(" \\n")
                line = re.sub(r"ADMAS\s*", "", line)
                line = re.sub(r"(-[0-9]+\.)", r" \1", line)
    
                values = [float(value) for value in line.split()]
                matrix.append(values)
    

    And output will be:

    [
    [8046.0, 0.008206, 15320.0, 0.1066, 6982000.0, -2.82], 
    [-0.006868, 200900.0, 0.01454, 951600.0, -1.209, 6058000.0], 
    [15430.0, 0.9179, 1459000.0, 5.463, 39180000.0, -29.04], 
    [-0.2267, 953700.0, 3.902, 30710000.0, -134.4, 107300000.0], 
    [7005000.0, 22.6, 39780000.0, 62.96, 7586000000.0, -2125.0], 
    [1.093, 6052000.0, -6.178, 106500000.0, -1416.0, 1941000000.0]
    ]
    

    Hope it addresses your problem!