Issue
I have a numpy array I'm trying to save using python3, but numpy.save()
breaks every time I try to use it. What do I need to do to save the array?
I tried this code:
saveFilePath = Path(fc.selected,saveFolder,saveFileName+'.npy')
saveFile = open(saveFilePath,'w')
# THIS IS THE ERROR; it should be 'wb', not 'w'; see answer by hpaulj
np.save(saveFile,vz[key1][key2][key3])
where saveFilePath and saveFile are the following: saveFile & saveFilePath In case it matters, the file saveFile does exist and does get created, it's where it's supposed to be, and it has 0 bytes in it (which I expect with a failed save command).
I got the following error: screencap of error Traceback
That honestly makes no sense since in earlier code I use this same exact method to save a different numpy array.
I've tried json.dump()
and pickle.dump()
and get errors and I would rather just not use them because of other issues those bring up.
The array looks like this:
This seems like it should be an extremely simple save. What's going on?
Solution
Inspired by the old post I found, I tried opening a file in 'w' mode, and got your error:
In [521]: f=open('foobar.npy','w')
In [522]: np.save(f, np.arange(10))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[522], line 1
----> 1 np.save(f, np.arange(10))
File <__array_function__ internals>:200, in save(*args, **kwargs)
File ~\miniconda3\lib\site-packages\numpy\lib\npyio.py:522, in save(file, arr, allow_pickle, fix_imports)
520 with file_ctx as fid:
521 arr = np.asanyarray(arr)
--> 522 format.write_array(fid, arr, allow_pickle=allow_pickle,
523 pickle_kwargs=dict(fix_imports=fix_imports))
File ~\miniconda3\lib\site-packages\numpy\lib\format.py:695, in write_array(fp, array, version, allow_pickle, pickle_kwargs)
659 """
660 Write an array to an NPY file, including a header.
661
(...)
692
693 """
694 _check_version(version)
--> 695 _write_array_header(fp, header_data_from_array_1_0(array), version)
697 if array.itemsize == 0:
698 buffersize = 0
File ~\miniconda3\lib\site-packages\numpy\lib\format.py:453, in _write_array_header(fp, d, version)
451 else:
452 header = _wrap_header(header, version)
--> 453 fp.write(header)
TypeError: write() argument must be str, not bytes
Correct save and load
In [528]: with open('foobar.npy','wb') as f:
...: np.save(f,np.arange(10))
...:
In [529]: np.load('foobar.npy')
Out[529]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
You neglected to show the full error message, and also did not tell us anything about the file object you are trying to use. For some reason you jump to the conclusion that the problem was with the array you tried to save, not the file you tried to save it to.
why
The error is a bit obscure because the dev's did not expect users to make this mistake. When the file is opened with 'w', all of its writes are supposed to be str
, not byte(string)
. But the save code formats the header as a bytestring. So the write
raises this error.
Usually np.save
gets a filename, and does its own open
. Saving to an already opened file is allowed, but it isn't paranoid enough to verify that the opened file had the right mode.
Answered By - hpaulj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.