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?
我正在尝试将固定宽度的二进制数据存储在numpy数组中。但是,如果我的数据具有尾随空值,则它们会被剥离。如果我使用void类型它们会工作,但我想将它们保存为字节字符串。有没有办法做到这一点?
>>> 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']]
1 个解决方案
#1
1
It dawned on me that I could side-step numpy's processing of the strings by specifying the type as an object.
我突然意识到,我可以通过将类型指定为对象来支持numpy对字符串的处理。
>>> np.array([(b'abc\x00\x00',), (b'de\x00\x00\x00',)], dtype='O')
[[b'abc\x00\x00']
[b'de\x00\x00\x00']]
#1
1
It dawned on me that I could side-step numpy's processing of the strings by specifying the type as an object.
我突然意识到,我可以通过将类型指定为对象来支持numpy对字符串的处理。
>>> np.array([(b'abc\x00\x00',), (b'de\x00\x00\x00',)], dtype='O')
[[b'abc\x00\x00']
[b'de\x00\x00\x00']]