2016-08-23 987 views
0

我试图直接在C++中读取npy文件(numpys mmap格式),但遇到了一些困难。从C++读入npy文件

该文件是使用numpy编写的,可以使用numpy轻松读回(所以它没有损坏或任何东西)。

我的第一次尝试是直接使用cnpy包(https://github.com/rogersce/cnpy),但是这会引发关于文件头大小的断言错误。

有没有人有一个在numpy中创建一个npy文件并在C++中读取它的例子?

干杯, 杰克

+0

我认为这将是明智的,用(A)一些广泛支持的矩阵的文件格式,例如[其中之一(http://docs.scipy.org/doc/scipy/reference/io。或者(B)写一些自定义的代码。 – sascha

+0

写入.npy的代码是Python,应该很容易确定头部大小。寻找类似'lib/format.py'的东西。如果你没有numpy,看看github仓库。 – hpaulj

回答

2

我会检查的前10个字节对的描述的测试文件中np.lib.format和C++代码

format.py文件文档头的局部引用

Format Version 1.0 
------------------ 

The first 6 bytes are a magic string: exactly ``\\x93NUMPY``. 

The next 1 byte is an unsigned byte: the major version number of the file 
format, e.g. ``\\x01``. 

The next 1 byte is an unsigned byte: the minor version number of the file 
format, e.g. ``\\x00``. Note: the version of the file format is not tied 
to the version of the numpy package. 

The next 2 bytes form a little-endian unsigned short int: the length of 
the header data HEADER_LEN. 

我还没有用另一种语言编写这个代码,但是确实想看看那些想在同一个文件中保存几个数组的SO的头文件。

loading arrays saved using numpy.save in append mode