首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从返回numpy数组的c++调用python函数时访问数据

如何从返回numpy数组的c++调用python函数时访问数据
EN

Stack Overflow用户
提问于 2019-06-26 01:47:04
回答 1查看 140关注 0票数 2

我正在通过pybind 11从c++调用python函数。python函数返回一个数值数组,我想在c++中分析数值数组中的数据。

代码语言:javascript
复制
//the code for testing
#include <pybind11/embed.h> // everything needed for embedding
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>

int main()
{

py::scoped_interpreter guard{};

py::print("test python interpreter");
auto sys = py::module::import("sys");
sys.attr("path").attr("append")(
"<path for python module>");

auto hdf5 = py::module::import("reader_hdf5");

auto rdata = hdf5.attr("load_next_chunk")(0, 1);

// how to access the data from the return value ?

 py::array_t<int16_t> array(331776);
 array = rdata.cast<py::array_t<int16_t>>();
}

这里的代码有错误:

代码语言:javascript
复制
terminate called after throwing an instance of 'pybind11::error_already_set'
what():  TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
EN

回答 1

Stack Overflow用户

发布于 2021-01-23 05:53:24

对mutable_data()的调用可以工作

代码语言:javascript
复制
    py::array_t<int>rdata= hdf5.attr("load_next_chunk")(0, 1);

    int *carray = rdata.mutable_data();

然后内容应该存在于carray中。我不是pybind专家,但是这个解决方案是由pybind github forum提供的

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56759489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档