Issue
I have some python that needs to send a pickled numpy array to stdout. If I try to
numpy.save(sys.stdout.buffer, data, allow_pickle=False)
I get the following error
Traceback (most recent call last):
File "/home/thoth/work/TandbergLabs/2022/iot-rendezvous/picture-server/src/main/resources/com/ericsson/duluthMadScience/pictureServer/histogram.py", line 29, in <module>
numpy.save(sys.stdout.buffer, data, allow_pickle=False)
File "<__array_function__ internals>", line 180, in save
File "/usr/lib/python3.10/site-packages/numpy/lib/npyio.py", line 502, in save
format.write_array(fid, arr, allow_pickle=allow_pickle,
File "/usr/lib/python3.10/site-packages/numpy/lib/format.py", line 689, in write_array
array.tofile(fp)
OSError: obtaining file position failed
What writable file-like object can I pass to numpy.save
that I can later fetch bytes from?
Solution
you can write to io.BytesIO
buffer then write it to stdout or even transfer it over network.
import numpy as np
import io
import sys
buffer = io.BytesIO()
arr = np.array([1, 2, 3])
np.save(buffer, arr, allow_pickle=False)
bytes_value = buffer.getvalue()
print(bytes_value)
sys.stdout.buffer.write(bytes_value) # prints it in non-human readable binary
Answered By - Ahmed AEK
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.