I have a bunch of files containing numpy arrays at some url (e.g., https://my_url/my_np_file.npy
) and I am trying to load them in my computer.
If I download the file manually, I can properly load the numpy array using np.load('file_path')
. If I take the url reponse (using the code below), save the content to a file and then use np.load()
, it also works.
response, content = http.request('https://my_url/my_np_file.npy')
If I try to load the array from the content string, I get the error bellow. This is likely because the np.load
is interpreting the string input as a name for the file, rather than the data itself.
File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 370, in load fid = open(file, "rb") TypeError: file() argument 1 must be encoded string without null bytes, not str
Is there any way to load the array without having to save a file?
You can save your NumPy arrays to CSV files using the savetxt() function. This function takes a filename and array as arguments and saves the array into CSV format.
all() in Python. The numpy. all() function tests whether all array elements along the mentioned axis evaluate to True.
NumPy contains a multi-dimensional array and matrix data structures. It can be utilised to perform a number of mathematical operations on arrays such as trigonometric, statistical, and algebraic routines. Therefore, the library contains a large number of mathematical, algebraic, and transformation functions.
You are missing io.BytesIO
to make the string appear like a file object to np.load
!
The following is what you're looking for:
import requests
import io
response = requests.get('https://my_url/my_np_file.npy')
response.raise_for_status()
data = np.load(io.BytesIO(response.content)) # Works!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With