替换文件中的行
replacing lines in file
我有一个看起来像这样的文件(ar 坐标 x、y、z 和线代表一些对象):
1.02 0.63 0.0003
-1.34 0.61 0.0002
0.0 0.0 0.0
-1.91 0.25 0.87
-1.32 1.70 0.0
0.02 -1.12 -0.06
我想:
1) 将第二行乘以 3;
2) 找出第 2 行的新值和相同列中的旧值 n(我通过乘法得到的)之间的差异(即第二行的第一个新值和旧值之间的差异,第二行的第二个新的列价值和旧等)
3) 用新值替换第 2 行中的值;
4) 将我得到的差异添加到第 4 行和第 5 行的值中。
所以输出应该是这样的:
1.02 0.63 0.0003
-4.02 1.83 0.0006
0.0 0.0 0.0
-4.59 1.47 0.8704
-4.00 2.92 0.0004
-2.66 0.10 -0.0596
到目前为止我得到的是:
import numpy as np
a=np.loadtxt('geometry.in')
C=s[1]
b=np.array((a)[C]) #read second line as array
x_old=b[0] #define coordinate x
y_old=b[1] #define coordinate y
z_old=b[2] #define coordinate z
C_new=b*3 #multiplying all line by 3
x=C_new[0] #defining new values in columns of the line
y=C_new[1]
z=C_new[2]
dx=x-x_old #the differene that I need to add to the first column of lines 4 and 5
dy=y-y_old
dz=z-z_old
我尝试了 a.replace(x_old, x) 但它不起作用,我真的陷入了困境。
如果您必须使用 numpy,可以使用以下示例:
将整个数据加载为 numpy 数组 "ao"
old2 = ao[1,:]
new2 = ao[1,:]*3
diff = new2 - old2
ao[1,:] = new2
ao[3:,:] = ao[3:,:] + diff
我有一个看起来像这样的文件(ar 坐标 x、y、z 和线代表一些对象):
1.02 0.63 0.0003
-1.34 0.61 0.0002
0.0 0.0 0.0
-1.91 0.25 0.87
-1.32 1.70 0.0
0.02 -1.12 -0.06
我想:
1) 将第二行乘以 3;
2) 找出第 2 行的新值和相同列中的旧值 n(我通过乘法得到的)之间的差异(即第二行的第一个新值和旧值之间的差异,第二行的第二个新的列价值和旧等)
3) 用新值替换第 2 行中的值;
4) 将我得到的差异添加到第 4 行和第 5 行的值中。
所以输出应该是这样的:
1.02 0.63 0.0003
-4.02 1.83 0.0006
0.0 0.0 0.0
-4.59 1.47 0.8704
-4.00 2.92 0.0004
-2.66 0.10 -0.0596
到目前为止我得到的是:
import numpy as np
a=np.loadtxt('geometry.in')
C=s[1]
b=np.array((a)[C]) #read second line as array
x_old=b[0] #define coordinate x
y_old=b[1] #define coordinate y
z_old=b[2] #define coordinate z
C_new=b*3 #multiplying all line by 3
x=C_new[0] #defining new values in columns of the line
y=C_new[1]
z=C_new[2]
dx=x-x_old #the differene that I need to add to the first column of lines 4 and 5
dy=y-y_old
dz=z-z_old
我尝试了 a.replace(x_old, x) 但它不起作用,我真的陷入了困境。
如果您必须使用 numpy,可以使用以下示例: 将整个数据加载为 numpy 数组 "ao"
old2 = ao[1,:]
new2 = ao[1,:]*3
diff = new2 - old2
ao[1,:] = new2
ao[3:,:] = ao[3:,:] + diff