How to store binary values ​​with trailing zeros in a numpy array?

I am trying to store fixed width binary data in a numpy array. However, if my data has trailing zeros, they are lost. They work if I use the void type, but I want to save them as strings of bytes. Is there any way to do this?

>>> import numpy as np

# Works
>>> varr = np.array([(b'abc\x00\x00',), (b'de\x00\x00\x00',)], dtype='V5')
[[[97 98 99  0  0]]
 [[100 101   0   0   0]]]

# Strips nulls
>>> sarr = np.array([(b'abc\x00\x00',), (b'de\x00\x00\x00',)], dtype='S5')
[[b'abc']
 [b'de']]
+4
source share
1 answer

It became clear to me that I can process multiple lines using numing, specifying the type as an object.

>>> np.array([(b'abc\x00\x00',), (b'de\x00\x00\x00',)], dtype='O')
[[b'abc\x00\x00']
 [b'de\x00\x00\x00']]
+1
source

All Articles