【发布时间】:2011-09-19 17:38:41
【问题描述】:
我正在尝试操作以下文件。
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) 应该包含 6 列时只包含 3 列。我如何告诉程序为每一步 j=0,1,.... 转移要打印的列。
对于 j = 0,程序应输出到前 3 列,对于 j = 1,程序应输出到后 3 列 (3,4,5),依此类推。
最后,len 函数给出了输入文件中的行数,但是,什么函数给出了文件中的列数?
谢谢。
【问题讨论】:
-
print 1, 2, 3然后print 1, 2, 3再次打印成两行...