如何在此循环的每个步骤中保存同一列(out
)中文本文件ctr_tr3.npt
中trib[PO4]
的值?
N=5
for i in xrange(N):
os.chdir( 'c:\\Users\\Hidraulica\\Desktop\\w2furnas' )
os.system( '"C:\\Users\\Hidraulica\\Desktop\\w2furnas\\w2.exe"' )
shutil.move('tsr_1_seg6.opt', 'tsr_1_seg6.txt')
data = np.genfromtxt('c:\\Users\\Hidraulica\\Desktop\\w2furnas\\tsr_1_seg6.txt', skip_header=11, names=True)
input=np.array(data['PO4'])
trib =np.genfromtxt('c:\\Users\\Hidraulica\\Desktop\\w2furnas\\ctr_tr3.npt', skip_header=2, names=True)
out=np.array(trib['PO4'])
out[i]= input[i]*0.75
答案 0 :(得分:0)
我不确定我是否理解正确但你不能只打开一次文件,一次更改所有五个值,然后保存文件?
类似的东西:
os.chdir( 'c:\\Users\\Hidraulica\\Desktop\\w2furnas' )
os.system( '"C:\\Users\\Hidraulica\\Desktop\\w2furnas\\w2.exe"' )
shutil.move('tsr_1_seg6.opt', 'tsr_1_seg6.txt')
data = np.genfromtxt('c:\\Users\\Hidraulica\\Desktop\\w2furnas\\tsr_1_seg6.txt', skip_header=11, names=True)
trib = np.genfromtxt('c:\\Users\\Hidraulica\\Desktop\\w2furnas\\ctr_tr3.npt', skip_header=2, names=True)
trib['P04'] = data['P04']*0.75
with open('c:\\Users\\Hidraulica\\Desktop\\w2furnas\\ctr_tr3.npt', 'r') as f:
h = ''.join(f.readline() for i in range(2))
with open('c:\\Users\\Hidraulica\\Desktop\\w2furnas\\ctr_tr3.npt', 'w') as f:
# first write header from before:
f.write(h)
# names from dtype plus newline:
f.write(' '.join(trib.dtype.names) + '\n')
# then data, separated with newlines:
f.write('\n'.join([' '.join(map(str,i)) for i in a]))