如何加载两个字符串并将其浮动到一个numpy数组中?

时间:2021-03-14 21:41:20

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

我需要以某种方式在文本和数字中加载numpy负载。

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 

2 个解决方案

#1


3  

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

如果把它弄糟的那行总是以EFF开头,那么你可以很容易地忽略这行:

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

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

它将把任何以“EFF”开头的行作为注释,并将被忽略。

#2


1  

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的skiprows参数。loadtxt来跳过标题。编写自定义代码来读取标题,因为它似乎有不规则的格式。

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

NumPy最适用于相同的数值数据——不要尝试将字符串放入其中。

#1


3  

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

如果把它弄糟的那行总是以EFF开头,那么你可以很容易地忽略这行:

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

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

它将把任何以“EFF”开头的行作为注释,并将被忽略。

#2


1  

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的skiprows参数。loadtxt来跳过标题。编写自定义代码来读取标题,因为它似乎有不规则的格式。

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

NumPy最适用于相同的数值数据——不要尝试将字符串放入其中。