我正在尝试操作以下文件。在循环中打印
1 2 -3 5 10 8.2
5 8 5 4 0 6
4 3 2 3 -2 15
-3 4 0 2 4 2.33
2 1 1 1 2.5 0
0 2 6 0 8 5
该文件只包含数字。
我正试图编写一个程序来减去彼此的行并将结果打印到文件中。我的程序在下面,dtest.txt
是输入文件的名称。程序的名称是make_distance.py
。
from math import *
posnfile = open("dtest.txt","r")
posn = posnfile.readlines()
posnfile.close()
for i in range (len(posn)-1):
for j in range (0,1):
if (j == 0):
Xp = float(posn[i].split()[0])
Yp = float(posn[i].split()[1])
Zp = float(posn[i].split()[2])
Xc = float(posn[i+1].split()[0])
Yc = float(posn[i+1].split()[1])
Zc = float(posn[i+1].split()[2])
else:
Xp = float(posn[i].split()[3*j+1])
Yp = float(posn[i].split()[3*j+2])
Zp = float(posn[i].split()[3*j+3])
Xc = float(posn[i+1].split()[3*j+1])
Yc = float(posn[i+1].split()[3*j+2])
Zc = float(posn[i+1].split()[3*j+3])
Px = fabs(Xc-Xp)
Py = fabs(Yc-Yp)
Pz = fabs(Zc-Zp)
print Px,Py,Pz
该方案是正确的计算值,但是,当我尝试调用程序写入输出文件,
mpipython make_distance.py > distance.dat
输出文件(distance.dat
)只包含3列时,它应该包含6.如何告诉程序将每列打印的列移动到每一步j = 0,1,...
对于j = 0,程序应该输出到前3列,对于j = 1程序应该输出到第二个3列(3,4,5)等等。
最后len
函数给出了输入文件中的行数,但是什么函数给出了文件中的列数?
谢谢。
'打印1,2,3'然后再'打印1,2,3'打印两行... – Griwes