aabceh
aabceh

Reputation: 95

Create Numpy array from list of arbitrary sized bites

How can I create a numpy array from a python list of bytes objects of an arbitrary (but known) size?

Example:

size = 10
byte_list = [np.random.default_rng().bytes(size) for i in range(100)]

numpy_array = # make array from byte_list

# do something with the array
test_vals = np.random.default_rng().choice(numpy_array, size=10)

I tried to do something like this, but got an error that it didn't understand 'B10' as a data type.

numpy_array = np.fromiter(byte_list, dtype=np.dtype(f'B{size}'), count=100)

Upvotes: 0

Views: 58

Answers (1)

Corralien
Corralien

Reputation: 120391

I think you should use S dtype and not B

numpy_array = np.fromiter(byte_list, dtype=np.dtype(f'S{size}'), count=100)
#                                              HERE --^     
# Unsigned byte (only one)
>>> np.dtype('B')
dtype('uint8')

# Byte string
>>> np.dtype('S10')
dtype('S10')

Upvotes: 1

Related Questions