我必须启动大量计算,并且每次都必须保存2D文件文本,因此我想将结果作为3D文本文件“实时”存储,每个切片对应一个计算结果。
第一次计算是可以的,但是当我执行第二次计算时,在“ np.loadtxt”步骤中,数组维数变为2D ...所以我无法达到目标...而且我无法进行整形当我开始标注尺寸时(...,...,1)
#MY FIRST RESULTS
test1 = open("C:/test.txt", "r")
test_r = np.genfromtxt(test, skip_header=1)
test_r = np.expand_dims(test_r, axis=2) #I create a new axis to save in 3D
test1.close()
#I test if the "Store" file to keep all my results is created.
try:
Store= np.loadtxt('C:/Store.txt')
except:
test=1
#If "Store" is not created, I do it or I concatenate in my file.
if test ==1:
Store= test_r
np.savetxt('C:/Store.txt', Store)
test=2
else:
Store = np.concatenate((Store,test_r), axis=2)
np.savetxt('C:/Store.txt', Store)
#MY SECOND RESULTS
test2 = open("C:/test.txt", "r")
test_r = np.genfromtxt(test, skip_header=1)
test_r = np.expand_dims(test_r, axis=2)
test2.close()
#I launch the procedure again to "save" the results BUT DURING THE LOADTXT STEP THE ARRAY DIMENSIONS CHANGE TO BECOME A 2D ARRAY...
try:
Store= np.loadtxt('C:/Store.txt')
except:
test=1
if test ==1:
Store= test_r
np.savetxt('C:/Store.txt', Store)
test=2
else:
Store = np.concatenate((Store,test_r), axis=2)
np.savetxt('C:/Store.txt', Store)
这是cPickle的示例:
import cPickle
# write to cPickle
cPickle.dump( thing_to_save, open( "filename.pkl", "wb" ) )
# read from cPickle
thing_to_save = cPickle.load( open( "filename.pkl", "rb" ) )
函数的"wb"
和"rb"
参数open()
很重要。CPickle以二进制格式写入对象,因此仅使用"w"
而"r"
不会起作用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句