我有一个文本文件,如下所示:1 1 2 1 1e82 1 2 3 1e53 2 3 2 20004 2 5 6 10005 2 4 3 1e46 3 6 4 50007 3 5 2 20008 3 2 3 50009 3 4 5 1e910 3 2 3 1e6我的问题是,如何更改一列(例如将所有数据从最后一列除以900)并将整个数据保存在一个新文件中?提前致谢
3 回答

千万里不及你
TA贡献1784条经验 获得超9个赞
您可以使用numpy库。下面的代码应该做到这一点。
import numpy as np
# assume the data is in.txt file
dat = np.loadtxt('in.txt')
# -1 means the last column
dat[:, -1] = dat[:, -1]/900
# write the result to the out.txt file
# fmt is the format while writing to the file
# %.2f means that it will save it to the 2 digits precision
np.savetxt('out.txt', dat, fmt='%.2f')

达令说
TA贡献1821条经验 获得超6个赞
对于文件的每一行,
vals = line.split(' ')
my_val = float(vals[4])/900
output = open('filename', 'wb')
output.write(my_val +'\n')
添加回答
举报
0/150
提交
取消