0

I want to read in a text file that is very poorly made in that the values in each line are sometimes not separated by spaces or commas (so i cant use .split()). I want to read it like you would in FORTRAN where I tell it exactly where each value is. This is what I am trying. Does anyone know a better approach to do this? Thanks !

f=open('f.out','r')

lines = f.readlines()

nLines = len(lines)
data = {}


keys = {'SPE':[0, 2, np.int],              #I2
      'SPEISO':[2, 3, np.int],         #I1
      'wnum':[3,15, np.float64],       #F12.6
      'S':[15, 25, np.float64],     #E10.3
      'Ecoeff':[25, 35, np.float64],     #E10.3
      'AGA':[35, 40, np.float64],     #F5.5
      'SGA':[40, 45, np.float64],     #F5.4
      'ELO':[45, 55, np.float64],     #F10.4
      'N'  :[55, 59, np.float64],     #F4.2
      'FSH':[59, 67, np.float64],     #F8.6
      'TRS':[67, 127, np.str],
      'IERR': [127, 133, np.int],
      'IEFF': [133, 145, np.str],
      'other': [145,160, np.str]  }

for k in keys:
  data[k] = np.zeros(nLines)

for i, l in enumerate(lines):
 print i
  for k in keys:
    print k
    data[[k][i]] = l.format(keys[k])
0

1 Answer 1

1

You might be able to use the read_fwf function from the pandas library.

Something like:

import pandas
pandas.read_fwf('f.out', 
   colspecs=[x[:2] for x in keys.values()],
   dtype=[x[2] for x in keys.values()]
   )
Sign up to request clarification or add additional context in comments.

2 Comments

Great! It looks like it worked. Now I just need to figure how this DataFrame object works so I can get my values into regular arrays.
You can consider the DataFrame as a number of columns, where each column represents a numpy array. You can also transform it to a numpy record array (dataframe.to_records()), but you probably want to use the nice features of the dataframe object itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.