使用标题从矩阵创建numpy数组

时间:2014-11-19 08:01:21

标签: python arrays numpy

我坚持从文件创建一个numpy数组。这是代码,其中short_logs.txt是带有标题(无行名称)的数据数组

所以矩阵和代码看起来像这样:

a  b c  d 
12 3 5  6
9  8 45 8

log_file = open('short_logs.txt')
samples = log_file.readline()
log_names = samples.split()

fields = zip(log_names, ['f8']*len(log_names))
fields_dtype = dtype(fields)

logs = loadtxt(log_file, dtype=fields_dtype)

但后来我得到了这个错误,我不明白为什么。

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
C:\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
195             else:
196                 filename = fname
--> 197             exec compile(scripttext, filename, 'exec') in glob, loc
198     else:
199         def execfile(fname, *where):

 c:\users\temp\tmp4iajle.py in <module>()
 17 
 18
---> 19 logs = loadtxt(log_file, dtype=fields_dtype)

C:\Users\numpy\lib\npyio.pyc in     loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin)
848             items = [conv(val) for (conv, val) in zip(converters, vals)]
849             # Then pack it according to the dtype's nesting
--> 850             items = pack_items(items, packing)
851             X.append(items)
852     finally:

C:\Users\numpy\lib\npyio.pyc in  pack_items(items, packing)
782             ret = []
783             for length, subpacking in packing:
--> 784                 ret.append(pack_items(items[start:start+length], subpacking))
785                 start += length
786             return tuple(ret)

C:\Users\numpy\lib\npyio.pyc in pack_items(items, packing)
773         """Pack items into nested lists based on re-packing info."""
774         if packing == None:
--> 775             return items[0]
776         elif packing is tuple:
777             return tuple(items)

IndexError: list index out of range`

有人能指出我正确的方向吗? 谢谢

0 个答案:

没有答案