Jeroen
Jeroen

Reputation: 841

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.

Upvotes: 0

Views: 1356

Answers (3)

Shubham Sharma
Shubham Sharma

Reputation: 71689

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!

Upvotes: 1

guialmachado
guialmachado

Reputation: 536

You should consider using Pandas for that.

python -m pip install pandas

Then you need to import pandas to you code.

import pandas as pd

then you need to locate the file and import it as a dataframe.

df = pd.read_fwf(YOURPATH + 'data.txt')
#And try to print its head to see if import was successfull
print(df.head())

At last, you should filter 'ADAM' on the dataframe's first column.

df.loc[df['column_name'] == ADAM]

Upvotes: 0

Phoenixo
Phoenixo

Reputation: 2113

It's not the cleanest, but it seems to work :

  • I create spaces before the minus - in order to split later between numbers
  • I remove spaces for E -, because of a side effect of previous point
  • I remove the \n hard written in text with replace(" \\n", "")
  • I remove the "next line" characters too \n
  • and I remove the 11 first characters (ex: ADMAS) with [11:]
filename = "data.txt"
string_fnd_1 = "ADMAS"
textfile = open(filename, 'r')
file_lines = textfile.readlines()
textfile.close()
new_file_lines = []
for li in file_lines:
    new_line = li.replace("-", " -").replace("E -", "E-").replace(" \\n", "").replace("\n", "")[11:]
    if string_fnd_1 in li:
        new_file_lines.append(new_line)
matches_admas = [line.split() for line in new_file_lines]

print(matches_admas)

Output :

[
    ['8.046E+03', '8.206E-03', '1.532E+04', '1.066E-01', '6.982E+06', '-2.820E+00'],
    ['-6.868E-03', '2.009E+05', '1.454E-02', '9.516E+05', '-1.209E+00', '6.058E+06'],
    ['1.543E+04', '9.179E-01', '1.459E+06', '5.463E+00', '3.918E+07', '-2.904E+01'],
    ['-2.267E-01', '9.537E+05', '3.902E+00', '3.071E+07', '-1.344E+02', '1.073E+08'],
    ['7.005E+06', '2.260E+01', '3.978E+07', '6.296E+01', '7.586E+09', '-2.125E+03'],
    ['1.093E+00', '6.052E+06', '-6.178E+00', '1.065E+08', '-1.416E+03', '1.941E+09']
]

Upvotes: 1

Related Questions