从用numpy.save(...)保存的文件将numpy数组加载到C中 [英] Load a numpy array into C from a file that was saved with numpy.save(...)

查看:245
本文介绍了从用numpy.save(...)保存的文件将numpy数组加载到C中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问一个C程序中的数据,该程序位于使用numpy.save(...)从python编写的文件中.到目前为止,我一直在寻找一种方法来调用我的C程序numpy.load的C版本,但是似乎没有可直接访问的C版本,因为numpy.load使用pickle模块.另一个解决方案是从C启动嵌入式python解释器,并在该解释器中调用numpy.load,这应返回numpy数组,然后可以按常规方式访问该数组.但是,最后一个方法看起来有点沉重,因为我必须使用解释器.是否有更好的方法将numpy数组直接加载到C中?

I'd like to access data in a C program that resides in a file that was written from python using numpy.save(...). So far, I was looking for a way to call the C version of numpy.load my C program, but it seems there is no directly accessible C version, because numpy.load uses the pickle module. Another solution would be to start an embedded python interpreter from C and call numpy.load in that interpreter, which should return the numpy array, which I can then access in the usual manner. However the last appraoch seems a bit to heavy, because I have to use the interpreter. Is there a better way to directly load the numpy array into C?

推荐答案

.npy格式并不是真正要从NumPy外部读取的格式.同时提供出色C和Python库的多种格式,例如HDF5和NetCDF,因此我看不出有任何理由可以从C访问本机NumPy文件.

The .npy format is not really meant to be read from outside NumPy. There are many formats available with provide both, excellent C and Python libraries, like HDF5 and NetCDF, so I can't see any reason whatsoever to access native NumPy files from C.

如果您仍然希望这样做,请参见 格式.除非您将Python对象存储在数组中,否则它不会在内部使用pickle(但是在这种情况下,我想知道您将如何处理C语言中的对象).

If you want to do this anyway, here are the docs of the .npy format. It does not use pickle internally unless you stored Python objects in your array (but in that case, I would wonder what you are going to do with those in your C program).

这篇关于从用numpy.save(...)保存的文件将numpy数组加载到C中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆