numpy的不同类型的数组保存到一个文本文件

问题描述:

说我有以下numpy的结构数组:numpy的不同类型的数组保存到一个文本文件

>>> a = numpy.array((1, 2.0, 'buckle_my_shoe'),dtype=('i4,f8,a14')) 
array((1, 2.0, 'buckle_my_shoe'), 
    dtype=[('f0', '<i4'), ('f1', '<f8'), ('f2', 'S14')]) 

,我想其保存到一个文本文件中一个空格或制表符分隔行。如果阵列都是相同的类型,我可以使用numpy.savetxt('myfile.dat,myarray,newline=" ")。然而,这似乎不喜欢混合数据类型/结构阵列,例如:

file('myfile.dat', 'a') 
numpy.savetxt('myfile.dat',a,newline=" ") 

给出了这样的错误:

IndexError: tuple index out of range 

谁能推荐这样做的呢?

编辑:我似乎无论出于何种原因不能到能独自离开这个答案,所以这里不使用不必要的csv模块更清洁的版本。记录,@ askewchan的答案仍然更好!

a = numpy.array([(1, 2.0, 'buckle_my_shoe'), 
       (3,4.0,'lock_the_door')],dtype=('i4,f8,a14')) 
with open('test.txt','w') as f: 
    f.write(' '.join([str(item) for sublist in a for item in sublist])) 
print open('test.txt','r').read() 

输出:

1 2.0 buckle_my_shoe 3 4.0 lock_the_door 

如果你有一个零d阵列像你的榜样,那么你可以这样做:

b = np.array((1, 2.0, 'buckle_my_shoe'), 
     dtype=[('f0', '<i4'), ('f1', '<f8'), ('f2', 'S14')]) 

with open('myfile.dat','w') as f: 
    for el in b[()]: 
     f.write(str(el)+' ') # or `f.write(repr(el)+' ') to keep the quote marks 

这是通过使用[()]访问0D数组的元素:

>>> b.ndim 
0 

>>> b[0] 
IndexError: 0-d arrays cannot be indexed 

>>> b[()] 
(1, 2.0, 'buckle_my_shoe') 

如果您定期使用零维的numpy阵列,为了使复杂的dtype,我可能会建议NamedTuple from collections

>>> import collections 
>>> A = collections.namedtuple('A', ['id', 'val', 'phrase']) 
>>> a = A(1, 2.0, 'buckle_my_shoe') 

>>> a 
A(id=1, val=2.0, phrase='buckle_my_shoe') 
>>> a.id 
1 
>>> a.val 
2.0 
>>> a.phrase 
'buckle_my_shoe' 

with open('myfile.dat','w') as f: 
    for el in a:  
     f.write(repr(el)+' ') 

如果阵列有一个以上的元素:

a = np.array([(1, 2.0, 'buckle_my_shoe'), 
       (3, 4.0, 'lock_the_door')], 
     dtype=('i4, f8, a14')) 

我不知道究竟你希望你的文件是什么样子。如果你想用空格分开的元组,这是我认为最好的办法:这会导致像文件

with open('myfile.dat','w') as f: 
    for row in a: 
     f.write(repr(row)+' ') 

(1, 2.0, 'buckle_my_shoe') (3, 4.0, 'lock_the_door') 

也许你想有没有逗号或圆括号在这种情况下,你可以这样做:

with open('myfile.dat','w') as f: 
    for row in a: 
     for el in row: 
      f.write(str(el)+' ') 

这给本文件:

1 2.0 buckle_my_shoe 3 4.0 lock_the_door 

使用repr保持qutoes周围的字符串

with open('myfile.dat','w') as f: 
    for row in a: 
     for el in row: 
      f.write(repr(el)+' ') 

这给本文件:

1 2.0 'buckle_my_shoe' 3 4.0 'lock_the_door' 

奖励:如果你的D型细胞具有字段名称,你可以打印这些:

a.dtype.names = "index value phrase".split() 
a.dtype 
#dtype([('index', '<i4'), ('value', '<f8'), ('phrase', 'S14')]) 

with open('myfile.dat','w') as f: 
    for name in a.dtype.names: 
     f.write(name + ' ') # or write(repr(name)) to keep the quote marks 
    for row in a: 
     for el in row: 
      f.write(repr(el)+' ') 

注意,如果你复制这些文件被警告我以前'w''a',这样我就可以覆盖每一个在我的测试案例。

+0

伟大的答案,但似乎没有一个0-d阵工作。例如。 '[f.write(str(el))for a']给出了一个'TypeError:对一个0-d数组的迭代'。虽然工作良好,但> 1排。 – atomh33ls 2013-04-08 15:22:26

+0

@ atomh33ls对不起,这是故意的,因为我认为0D数组是个错误。我修正了这个问题。 – askewchan 2013-04-08 15:51:14

+0

@ atomh33ls注意我的建议是使用命名元组而不是0D numpy数组,如果你经常这样做的话。 – askewchan 2013-04-08 15:53:30