how to store bytes-arrays with numpy arrays

Question:

I’m writing a project where I need to store 2d arrays of bytes. I used for that purpose nympy arrays, but I just realized that if a byte-array ends with x00 character, numpy truncate the filal character. Is there a way to avoid that?

Es.

>>> a = np.array([b'abcx00'])
>>> print(a[0])
b'abc'

I expected that a[0] is [b'abcx00']

Asked By: fprefect

||

Answers:

All you should do is to specify the type of the array to dtype=object:

a = np.array([b'abcx00'],dtype=object)

Output:

b'abcx00'
Answered By: Hamzah

You should always give way more information about what’s surrounding your code, but assuming np is just numpy and nothing weird is happening, your array shouldn’t be truncated.
‘/x00’ is not a printable character, in fact in many languages it’s the string terminator.

Anyway, try calling

print(a.itemsize)

and you will notice the byte is still there.

Answered By: sgargoyle777

Using an explicit dtype while generating the numpy array is one technique to prevent the truncation of the final x00 character when using numpy arrays. The np.array method allows you to specify the array’s data type with the dtype argument. For instance:

>>> a = np.array([b'abcx00'], dtype='|S4')
>>> print(a)
[b'abcx00']

Here, the dtype is specified as '|S4', which means the elements of the array will be null-terminated strings of maximum length 4.

Answered By: Rohit Singh
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.