Search code examples
pythonreadlines

Python: Read specific element from a matrix written in a file with string


I would like to read data from a matrix in a file, the problem is the data is float with string, however, I am interested only in reading the float element, I tried with several options including pandas dataframe, however, I could not so I am kindly asking for help.

I am attaching the file as picture as I could not upload the file itself, and the code:

with open('data.dat', 'r') as f:
output_data = f.readlines()
element =  [(d+' ')[:d.find(':')].rstrip() for d in output_data] # trying to read only number after ":" 
print(output_data[0]) 
 # EXPECTED OUTPUT is: 
  0.162239678E+01
 # CURRENT OUTPUT IS: ['  data  1:           0.162239678E+01\n', '  data  2:           0.413951524E+01']



## data.dat    is as the following:
  data  1:           0.162239678E+01
  data  2:           0.413951524E+01

Solution

  • It seems like you have the correct approach, but somehow you're targeting the first part of each line instead of the actual number. The problem is this: [:d.find(':')], you want the other side of the string: [d.find(':'):]. Also, you'll be capturing the ":" character, so add 1 to the position. Then, use str.strip instead of str.rstrip to apply the function on both sides of the string. This is the resulting code (with some simplifications):

    with open('data.dat', 'r') as f:
        output_data = f.readlines()
        elements =  [d[d.find(':')+1:].strip() for d in output_data]
        print(elements)