简体   繁体   English

带有Numpy的Python C扩展

[英]Python C Extension with Numpy

I'm trying to create a C Extension for Python with Numpy and have some problems reading the data from Numpy in my C code. 我正在尝试使用Numpy为Python创建C扩展,并且在我的C代码中从Numpy读取数据时遇到一些问题。

If I create a simple array like this in Python I'm able to read the values in the C code: 如果我在Python中创建像这样的简单数组,则可以读取C代码中的值:

Python: 蟒蛇:

from numpy import *
myarray = zeros([5, 20], dtype=uint32)

C: C:

value = (unsigned long*)PyArray_GETPTR2(myarray,0,0);    

The problem is when I try to read the value from the following Numpy Array: 问题是当我尝试从以下Numpy数组读取值时:

Python: 蟒蛇:

from numpy import *
myarray = zeros([5], dtype=[('f1', 'S16'), ('f2', 'S16'), ('f3', uint64), ('f4', uint32)] )

C: C:

value = (void*)PyArray_GETPTR1(myarray,0);

What kind og data type is the value in this case? 在这种情况下,值是哪种数据类型?

Numpy structured data types are by default equivalent to C packed structs. 默认情况下,Numpy结构化数据类型等效于C压缩结构。 They can, however, also be more complicated. 但是,它们也可能更加复杂。

To access the fields, check eg myarray.dtype.fields['f3'] , which in your case is (dtype('uint64'), 32) . 要访问这些字段,请检查例如myarray.dtype.fields['f3'] ,在您的情况下为(dtype('uint64'), 32) You should be able to access the corresponding data via (npy_uint64*)(((char*)value) + 32) . 您应该能够通过(npy_uint64*)(((char*)value) + 32)来访问相应的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM