开发者

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.

If I create a simple array like this in Python I'm able to read the values in the C code:

Python:

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

C:

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

The proble开发者_如何学编程m is when I try to read the value from the following Numpy Array:

Python:

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

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. They can, however, also be more complicated.

To access the fields, check e.g. myarray.dtype.fields['f3'], which in your case is (dtype('uint64'), 32). You should be able to access the corresponding data via (npy_uint64*)(((char*)value) + 32).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜