I have a textfile with data which I am trying to read in Python:
OMEGA2 1.450E+00 1.500E+00 1.550E+00 1.600E+00 1.650E+00 1.700E+00
OMEGA2 1.800E+00 1.850E+00 1.900E+00 1.950E+00 2.000E+00 2.050E+00
F2REAL 1.146E+00 -1.015E+03-2.206E+03-2.618E+03-2.288E+03-1.400E+03
F2REAL 6.255E+00 -3.254E+02-8.150E+02-1.060E+03-9.749E+02-5.995E+02
F2REAL 1.754E+01 -1.530E+02-4.375E+02-5.932E+02-5.618E+02-3.536E+02
F2REAL 1.740E+01 -7.981E+01-2.525E+02-3.748E+02-3.891E+02-2.739E+02
OMEGA2 1.800E+00 1.850E+00 1.900E+00 1.950E+00 2.000E+00 2.050E+00
Now, I only want to have values where the line start with F2REAL; Per line, I want to extract 6 values. Value1 is from index 11 to index 20, value to from index 21 to 30, ..., value 6 is from index 61:70
I tried the following:
file = 'file.txt'
STR1 = 'F2REAL'
def get_data():
with open(file) as f:
hyd_all = f.readlines()
for line in hyd_all:
if STR1 in line:
print([float(line[10:19]),float(line[20:29])])
get_data()
- This does not read the E-power, as I get [1.146,-1.015,..]. How do I get it correctly?
- Is there a better way instead of writing 10:19,20:29,..60:69 ? All lines of interest have 6 columns and always start at 10*i
- I want to append each result to a matrix. In this example of 4 rows and 6 columns