[英]save output of each iteration of for loop in single file
hii experts i have a text file(input.txt) that contain 5 columns data as given below:嗨专家我有一个包含 5 列数据的文本文件(input.txt),如下所示:
2 3 4 5 6
3 4 5 6 7
2 3 4 5 6
6 7 8 9 0
3 3 3 3 3
i just want to read single column at each iteration and want to add 5 on it and want np.savetxt to save each iteration output of the loop in different column of a text file我只想在每次迭代时读取单列并想在其上添加 5 并希望 np.savetxt 将循环的每个迭代输出保存在文本文件的不同列中
i wrote the script我写了脚本
import numpy as np
import random
inpdata=np.loadtxt("input.txt")
for x in np.arange(0,4,1):
data=inpdata[:,x]
sum=data+5
np.savetxt('tx_'+str(x),data)
while doing this output are saved in different files but i need output to be saved in single file as firstcolumn,second column, third column.... i need output like as below在执行此输出时保存在不同的文件中,但我需要将输出保存在单个文件中作为第一列、第二列、第三列......我需要如下输出
7 8 9 10 11
8 9 10 11 12
7 8 9 10 11
11 12 13 14 5
8 8 8 8 8
i hope some expert will help me .Thanks in advance.我希望一些专家会帮助我。提前致谢。
Since you just perform some manipulation on an array.因为您只是对数组执行一些操作。 You could equivalently do the following:
您可以等效地执行以下操作:
In this specific example, you could just add a scalar to you array like the following:在这个特定示例中,您可以向数组添加一个标量,如下所示:
import numpy as np
data=np.loadtxt("test.txt")
data += 5
np.savetxt('output.txt', data)
And if you insist to do it column-wise:如果您坚持按列进行操作:
import numpy as np
data=np.loadtxt("test.txt")
for i in range(data.shape[1]):
data[:, i] += 5
np.savetxt('output.txt', data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.