我正在嘗試操作以下文件。在循環中打印
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