-2

如何在此循环的每个步骤out中将文本文件ctr_tr3.npt中的值保存在同一列(in 中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
4

1 回答 1

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]))
于 2013-09-12T19:24:07.197 回答