调整大小的代码numpy.memmap
似乎有效,但程序所做的更改不会保存。
def test_resize_inplace():
fA = np.memmap('A_r.npy', dtype='uint8', mode='w+', shape=(3,12))
print "fA"
print fA
fA[2][0] = 42
# resize by creating new memmap
new_fA = np.memmap('A_r.npy', mode='r+', dtype='uint8', shape=(20,12))
print 'fA'
print fA
print 'new_fA'
print new_fA
同样,当我尝试调整大小程序时,它会在线粉碎 python 解释器
print new_fA
下面的代码:
def resize_memmap(fm,sz,tp):
fm.flush()
print fm.filename
new_fm = np.memmap(fm.filename, mode='r+', dtype= tp, shape=sz)
return new_fm
def test_resize_inplace():
fA = np.memmap('A_r.npy', dtype='uint8', mode='w+', shape=(3,12))
print "fA"
print fA
fA[2][0] = 42
sz= (20,12)
tp= 'uint8'
new_fA= resize_memmap(fA,sz,type)
new_fA[9][9]= 111
print 'fA'
print fA
print 'new_fA'
print new_fA
更新:
我试过.flush()
def test_memmap_flush():
fA = np.memmap('A_r.npy', dtype='uint8', mode='w+', shape=(3,12))
print "fA"
print fA
fA[2][0] = 42
fA.flush()
# fB = np.memmap('A_r.npy', dtype='uint8', mode='w+', shape=(3,12)) #fails
fB = np.memmap('A_r.npy', dtype='uint8', mode='r+', shape=(3,12))
print "fB"
print fB
print "done"
但我不明白为什么我不能有w+
模式?
IOError:[Errno 22] 无效模式('w+b')或文件名:'A_r.npy'
更新:
好的,我明白了。w+
用于创作和r+
读写。