写作&读ndarray

时间:2015-09-24 15:02:20

标签: python arrays numpy

我想保存一个形状为(5,2)的数组,该数组名为sorted_cube_station_list。

在打印中它看起来没问题,但是当我用numpy.tofile保存它,然后用numpy.fromfile读取它时它就成了一个数组

你可以帮帮我吗?     import numpy as num

nx=5
ny=5
nz=5
stations=['L001','L002','L003','L004','L005']

for x in range(nx):
    for y in range (ny):
         for z in range (nz):
             cube_station_list = []
             i=-1
             for sta in stations:
                 i=i+1
                 cube=[int(i), num.random.randint(2500, size=1)[0]]
                 cube_station_list.append(cube)
             cub_station_list_arr=num.asarray(cube_station_list)
             sorted_cube_station_list_arr=cub_station_list_arr[cub_station_list_arr[:, 1].argsort()]
             print x,y,z, sorted_cube_station_list_arr
             num.ndarray.tofile(sorted_cube_station_list_arr,str(x)+'_'+str(y)+'_'+str(z)

1 个答案:

答案 0 :(得分:6)

我建议您使用np.save

a = np.ones(16).reshape([8, 2])
np.save("fileName.npy", a)

请参阅the docs:第一个参数不能是您要保存的变量,而是要保存的文件的路径。因此,使用np.save(yourArray)

时出现的错误

您可以使用np.load(pathToArray)

加载已保存的数组
相关问题