输入和输出¶
日期 | 2011-11-17(最后修改),2006-05-20(创建) |
---|
简介¶
本页提供了一些示例,说明如何将 !NumPy 数组读入或写入文件,无论是 ASCII 还是二进制文件。演示的各种方法都有大量且有时复杂的选项,调用帮助以获取详细信息。
我们将考虑一个简单的示例,其中我们创建一个名为 `data` 的全零数组,将其写入文件 `myfile.txt`(对于二进制情况,为 myfile.dat),并将其读入 `read_data`。
本文档可以通过讨论更复杂的情况(例如多个数组)以及讨论所呈现的各种方法的成本/效益来改进。
文本文件¶
SciPy¶
可以使用 `savetxt` 来写入文件。到目前为止,读取文本数据的最简单方法是通过 genfromtxt(或派生便利函数 recfromtxt 和 recfromcsv)。
>>> from numpy import *
>>> data = zeros((3,3))
>>>#Write data:
>>> savetxt("myfile.txt", data)
>>>#Read:
>>> data = genfromtxt("myfile.txt") }}}
== Matplotlib (pylab) ==
Matplotlib provides an easy solution which seems to load data faster than read_array:
{{{#!python numbers=disable
>>> from numpy import *
>>> from pylab import load # warning, the load() function of numpy will be shadowed
>>> from pylab import save
>>> data = zeros((3,3))
>>> save('myfile.txt', data)
>>> read_data = load("myfile.txt")
numPy¶
>>> savetxt('myfile.txt', data, fmt="%12.6G") # save to file
>>> from numpy import *
>>> data = genfromtxt('table.dat', unpack=True)
def read_array(filename, dtype, separator=','):
""" Read a file with an arbitrary number of columns.
The type of data in each column is arbitrary
It will be cast to the given dtype at runtime
"""
cast = N.cast
data = [[] for dummy in xrange(len(dtype))]
for line in open(filename, 'r'):
fields = line.strip().split(separator)
for i, number in enumerate(fields):
data[i].append(number)
for i in xrange(len(dtype)):
data[i] = cast[dtype[i]](data[i])
return N.rec.array(data, dtype=dtype)
然后可以使用相应的 dtype 调用它
mydescr = N.dtype([('column1', 'int32'), ('column2Name', 'uint32'), ('col3', 'uint64'), ('c4', 'float32')])
myrecarray = read_array('file.csv', mydescr)
>>> numpy.save('test.npy', data)
>>> data2 = numpy.load('test.npy')
您可以使用 . 将多个数组保存在单个文件中。加载 . 文件时,您将获得一个类型为 . 的对象。您可以像这样获取数组列表并加载单个数组
>>> numpy.savez('foo.npz', a=a,b=b)
>>> foo = numpy.load('foo.npz')
>>> foo.files
['a', 'b']
>>> a2 = foo['a']
>>> b2 = foo['b']
>>> from scipy.io.numpyio import fwrite, fread
>>> data = zeros((3,3))
>>>#write: fd = open('myfile.dat', 'wb')
>>> fwrite(fd, data.size, data)
>>> fd.close()
>>>#read:
>>> fd = open('myfile.dat', 'rb')
>>> datatype = 'i'
>>> size = 9
>>> shape = (3,3)
>>> read_data = fread(fd, size, datatype)
>>> read_data = data.reshape(shape)
或者,您可以简单地使用 和 。 以下面的示例为例
>>> data.tofile('myfile.dat')
>>> fd = open('myfile.dat', 'rb')
>>> read_data = numpy.fromfile(file=fd, dtype=numpy.uint8).reshape(shape)
numpy data type. The option {{{fromfile(..., count=<number>)}}} specifies the number of data entries of that type you want to read in (the default -1 means read in the whole file, which is what you usually want). However, the method is not recommended for data storage and transfer between different platforms, since no byteorder and datatype information is stored (see also the docstrings).
If you want that, use {{{numpy}}}'s own binary file format. See {{{numpy.save}}}, {{{numpy.savez}}} and {{{numpy.load}}}.
{{{#! python numbers=disable
>>> numpy.save('test.npy', data)
>>> data2 = numpy.load('test.npy')
另一种(但已弃用)方法是完全控制字节序(字节顺序)、存储顺序(行优先、列优先)以用于秩 > 1 数组和写入和读回的数据类型。 写入
>>> from scipy.io import npfile
>>> shape = (3,3)
>>> data = numpy.random.random(shape)
>>> npf = npfile('test.dat', order='F', endian='<', permission='wb')
>>> npf.write_array(data)
>>> npf.close()
以及读回
>>> npf = npfile('test.dat', order='F', endian='<', permission='rb')
>>> data2 = npf.read_array(float, shape=shape)
>>> npf.close()
>>> from numpy.lib import format
>>> help(format)
这是一个最小的 C 示例 `cex.c`
#include"npy.h"
int main(){
double a[2][4] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 } };
int shape[2] = { 2, 4 }, fortran_order = 0;
npy_save_double("ca.npy", fortran_order, 2, shape, &a[0][0]);
return 0;
}
该程序创建一个文件 `ca.npy`,您可以以通常的方式将其加载到 Python 中。
>>> ca = np.load('ca.npy')
>>> print ca
[[ 1. 2. 3. 4.]
[ 5. 6. 7. 8.]]
相应的 Fortran 程序 `fex.f95` 如下所示
program fex
use fnpy
use iso_c_binding
implicit none
integer :: i
real(C_DOUBLE) :: a(2,4) = reshape([(i, i=1,8)], [2,4])
call save_double("fa.npy", shape(a), a)
end program fex
但 NumPy 数组的条目现在遵循 Fortran(列优先)顺序。
>>> fa = np.load('fa.npy')
>>> print fa
[[ 1. 3. 5. 7.]
[ 2. 4. 6. 8.]]
源代码分发中的 `README` 文件解释了如何使用 `make` 编译库。
如果您将 `npy.h` 和 `libnpy.a` 放置在与 `cex.c` 相同的目录中,那么您可以使用以下命令构建可执行文件 `cex`
gcc -o cex cex.c libnpy.a
类似地,将 `npy.mod` 和 `libnpy.a` 放置在与 `fex.f95` 相同的目录中,使用以下命令构建 `fex`
gfortran -o fex fex.f95 libnpy.a
部分作者:Unknown[5],VincentNijs,Unknown[56],FredericPetit,Elby,MartinSpacek,Unknown[57],Unknown[58],Unknown[53],AMArchibald,Unknown[59],Unknown[60],Unknown[61],Unknown[62],MikeToews