Home > database >  how to store bytes-arrays with numpy arrays
how to store bytes-arrays with numpy arrays

Time:01-31

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'abc\x00'])
>>> print(a[0])
b'abc'

I expected that a[0] is [b'abc\x00']

CodePudding user response:

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

a = np.array([b'abc\x00'],dtype=object)

Output:

b'abc\x00'

CodePudding user response:

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.

CodePudding user response:

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'abc\x00'], dtype='|S4')
>>> print(a)
[b'abc\x00']

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

  • Related