标签:python arrays numpy concatenation text
我必须启动大量的计算,每次都要保存一个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" ) )
open()函数的“wb”和“rb”参数很重要. CPickle以二进制格式写入对象,因此仅使用“w”和“r”将不起作用.
标签:python,arrays,numpy,concatenation,text 来源: https://codeday.me/bug/20190724/1526457.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。