python写入文本文件的数据类型必须是_用Python读写固定格式(MODFLOW)文本文件...

我尝试使用python读取、操作和写入文本文件。这些文件包含数字矩阵,由FORTRAN地下水流代码MODFLOW生成,形状不寻常,因为矩阵行被拆分为多个文件行,因此每行的值不超过7个。因此,一个包含37列的矩阵行输出为5行7个值(fmt=“%14.6E”),然后是1行2个值。下一个矩阵行从新行开始。在

我试图读取两个这样的文件,每个文件有730个时间步x 49行x37个列(大约18MB)。然后我想将数据以元素的方式相乘,并将结果写入具有相同格式的新文件中。在

我可以一行一行地用csv.reader然后numpy.savetext但速度非常慢。我怎么能用numpy(或类似的)来做,这样会更快?谢谢!在

更新:

我就快到了,只需要去掉输出文件中的逗号。显然,这是目前不可能的熊猫,所以我可能要做一个单独的手术。在

已解决:

以文本形式获取pandas输出并使用replace()删除分隔符。还是很快。在import pandas as pd

root = 'Taupo'

rctrans = read_csv(root+'._rctrans', header=None, delim_whitespace=True)

rcmult = read_csv(root+'._rcmult', header=None, delim_whitespace=True)

# duplicate rcmult nsteps times to make it the same size as rctrans

nsteps = len(rctrans.index)/len(rcmult.index)

rcmult = pd.concat([rcmult]*nsteps, ignore_index=True)

# multiply the arrays

rctrans = pd.DataFrame(rctrans.values*rcmult.values, columns=rctrans.columns, index=rctrans.index)

# write as csv with no delimiter

with open(root+'._rc','w') as w:

w.write(rctrans.to_csv(header=False, index=False, float_format='%14.6E').replace(',',''))

你可能感兴趣的:(python写入文本文件的数据类型必须是_用Python读写固定格式(MODFLOW)文本文件...)