2014-10-28 75 views
0

所以我有这样的阵列,说这是写一个数组到一个文本文件

import numpy as np 
m = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) 

,我想它的尺寸写入一个文本文件,然后我想写阵列本身低于。

我已经与

with open(filepath, 'w') as f: 
    n = len(m) 
    f.write(str(n) + ' \n') 
    np.savetxt(f, m) 

完成这个有些问题是该文件中的数字写为花车,我想他们,因为他们给予 - 为整数!据我可以告诉savetxt()没有这个设置。

我试过从使用savetxt()切换到使用tofile(),但这甚至不会将数组写入文件,我认为是因为我先写了其他东西到文件中,这是不知何故的与其运作。

我想我可以通过做一个for循环来写它,但是这似乎不如仅仅有一个函数在那里等待我使用它,如果存在的话。

回答

0

我不知道如果这是你在找什么,但它节省了数组,因为它是在这种情况下,元素都是浮动:

import numpy as np 
m= np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) 

filepath = '/Path/to/file/' 
fout = open(filepath + 'fout.dat', 'w') 

# I'd use this if you want the full shape of the array 
dimensions = np.shape(m) 
print >>fout, str(dimensions) + ' \dimensions' 
print >>fout, m 

fout.close() 

希望这有助于..

相关问题