【问题标题】:Read in npy file from C++ [closed]从 C++ 读入 npy 文件 [关闭]
【发布时间】:2016-12-30 03:23:57
【问题描述】:

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

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

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

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

干杯, 杰克

【问题讨论】:

  • 我认为明智的做法是使用 (A) 一些广泛支持的矩阵文件格式,例如 one of these 或 (B) 编写一些自定义代码。
  • 编写 .npy 的代码是 Python,应该很容易确定标头大小。寻找类似lib/format.py 的东西。如果没有 numpy,请查看 github 存储库。

标签: python c++ numpy mmap


【解决方案1】:

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

来自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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多