On Mon, Feb 12, 2018 at 6:13 AM, Eugen Wintersberger <
> Hi there,
> I have a question concerning the numpy iterator C-API. I want to create a
> string array using NPY_OBJECT as a datatype for creating the array (the
> reason I am going for this
> approach is that I do not know the length of the individual strings at the
> time I construct
> the array, I only know its shape). The original strings are stored
> in a std::vector<char*> instance. The approach I took was something like
> std::vector<char*> buffer = ....;
> NpyIter *iter = NpyIter_New(numpy_array,
> NPY_ITER_READWRITE | NPY_ITER_C_INDEX |
> NPY_CORDER , NPY_NO_CASTING,nullptr);
> NpyIter_IterNextFunc *iternext = NpyIter_GetIterNext(iter,nullptr);
> if(iternext == NULL)
> std::cerr<<"Could not instantiate next iterator function"<<std::endl;
> PyObject **dataptr = (PyObject**)NpyIter_GetDataPtrArray(iter);
> for(auto string: buffer)
> dataptr = PyString_FromSting(string); // this string construction
> seem to work
> This code snippet is a bit stripped down with all the safety checks
> removed to make
> things more readable.
> However, the array I get back still contains only a None instance. Does
> anyone have an idea
> what I am doing wrong here?
> Thanks in advance.
> best regards
Honestly, given that you're iterating over a single 1D array you've just
constructed, I don't think I would even bother trying to use the iterator.
For this case, a simple loop will be both concise and clear.
NumPy-Discussion mailing list