I'm trying to store fixed width binary data in a numpy array. However, if my data has trailing nulls, they get stripped. They do work if I use the void type, but I want to keep them as byte strings. Is there a 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']]
It dawned on me that I could side-step numpy's processing of the strings by 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']]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.