2016-08-23 986 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