【发布时间】:2013-05-12 21:34:06
【问题描述】:
我需要以某种方式在文本和数字中加载 numpy。
我收到此错误:
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
因为我正在加载的文件中有文本。我需要将每个单词及其下方的数据存储在数组索引中。我该怎么做?
编辑:抱歉没有举个例子。这是我的文件的样子。
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
这里显示的数字下方有数千个数字。此外,文件中有不同的数据集,因此您在顶部看到的标题重复,然后是一组新的新数字。
失败的代码:
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
【问题讨论】:
-
您需要提供有关文件结构的更多详细信息。
-
文件有多大?你能把整个东西加载到内存中吗?
-
我想是的。我只是尝试使用列表,并且 readlines() 没有给出错误。我想这意味着它不是太大?
-
您可以通过查看标题开始的行将文件拆分为单独的块,然后将它们加载到 numpy 以创建单独的数组。或类似的东西。 . .
标签: python arrays list numpy io