1

I'm trying to save (using savez_compressed) a bunch of numpy arrays into a BytesIO object and then load them back to a variable. So far, I can save the array's using following code

# Arrays
a = numpy.random.uniform(size=(10,10)) # dtype is float64
b = numpy.random.uniform(size=(10,10)) # dtype is float64

# Create bytes object
buf = io.BytesIO()

# Save the arrays into the buffer
numpy.savez_compressed(buf, a=a, b=b)

# Closed the buffer
buf.close()

I've been trying different methods to load them back. For example

ab = numpy.frombuffer(buf.read(), dtype='float64')

which raises ValueError: buffer size must be a multiple of element size. And trying to load as one would if it was a file

ab = numpy.load(buf)

raises ValueError: Cannot load file containing pickled data when allow_pickle=False and when buf.read() i get ValueError: embedded null byte.

1
  • Btw. To close the buffer, you sould use buf.close(), and not buf.seek() Commented Nov 21, 2019 at 10:05

1 Answer 1

1

The following works for me [Numpy version: numpy==1.16.1]:

import numpy
import io

# Arrays
a = numpy.random.uniform(size=(10,10)) # dtype is float64
b = numpy.random.uniform(size=(10,10)) # dtype is float64

# Create bytes object
buf = io.BytesIO()

# Save the arrays into the buffer
numpy.savez_compressed(buf, a=a, b=b)
buf.seek(0)

ab = numpy.load(buf)
print(ab['a'])
print(ab['b'])

# Closed the buffer
buf.close()
Sign up to request clarification or add additional context in comments.

2 Comments

It works!! Although, it doesn't work with my original arrays but they are of dtype "O". I guess that's the problem...
It was the dtype. Thanks!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.