hey mordecai
hey mordecai

Reputation: 69

How do I load both Strings and floats into a numpy array?

I need to somehow make numpy load in both text and numbers.

I am getting this error:

Traceback (most recent call last):
  File "ip00ktest.py", line 13, in <module>
    File = np.loadtxt(str(z[1]))        #load spectrum file 
  File "/usr/lib64/python2.6/site-packages/numpy/lib/npyio.py", line 805, in loadtxt
    items = [conv(val) for (conv, val) in zip(converters, vals)]
ValueError: invalid literal for float(): EFF

because my file I'm loading in has text in it. I need each word to be stored in an array index as well as the data below it. How do I do that?

Edit: Sorry for not giving an example. Here is what my file looks like.

FF   3500.  GRAVITY 0.00000  SDSC GRID  [+0.0]   VTURB 2.0 KM/S    L/H 1.25                            
  wl(nm)    Inu(ergs/cm**2/s/hz/ster) for 17 mu in 1221 frequency intervals
            1.000   .900  .800  .700  .600  .500  .400  .300  .250  .200  .150  .125  .100  .075  .050  .025  .010
    9.09 0.000E+00     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
    9.35 0.000E+00     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
    9.61 0.000E+00     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
    9.77 0.000E+00     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
    9.96 0.000E+00     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0

There are thousands of numbers below the ones shown here. Also, there are different datasets within the file such that the header you see on top repeats, followed by a new set of new numbers.

Code that Fails:

import sys
import numpy as np
from math import *

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

z = np.array(sys.argv)          #store all of the file names into array

i = len(sys.argv)           #the length of the filenames array

File = np.loadtxt(str(z[1]))        #load spectrum file 

Upvotes: 2

Views: 1612

Answers (2)

askewchan
askewchan

Reputation: 46578

If the line that messes it up always begins with EFF, then you can ignore that line quite easily:

np.loadtxt(str(z[1]), comments='EFF')

Which will treat any line beginning with 'EFF' as a comment and it will be ignored.

Upvotes: 3

Janne Karila
Janne Karila

Reputation: 25207

To read the numbers, use the skiprows parameter of numpy.loadtxt to skip the header. Write custom code to read the header, because it seems to have an irregular format.

NumPy is most useful with homogenous numerical data -- don't try to put the strings in there.

Upvotes: 1

Related Questions