[英]How can I store binary values with trailing nulls in a numpy array?
I'm trying to store fixed width binary data in a numpy array. 我正在尝试将固定宽度的二进制数据存储在numpy数组中。 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. 如果我使用void类型它们可以工作,但我想将它们保存为字节字符串。 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. 我突然意识到,我可以通过将类型指定为对象来支持numpy对字符串的处理。
>>> np.array([(b'abc\x00\x00',), (b'de\x00\x00\x00',)], dtype='O')
[[b'abc\x00\x00']
[b'de\x00\x00\x00']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.