开发者

Printing in a loop

I have the following file I'm trying to manipulate.

 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

The file just contains numbers.

I'm trying to write a program to subtract the rows from each other and print the results to a file. My program is below and, dtest.txt is the name of the input file. The name of the program is 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 = floa开发者_高级运维t(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

The program is calculating the values correctly but, when I try to call the program to write the output file,

mpipython make_distance.py > distance.dat

The output file (distance.dat) only contains 3 columns when it should contain 6. How do I tell the program to shift what columns to print to for each step j=0,1,....

For j = 0, the program should output to the first 3 columns, for j = 1 the program should output to the second 3 columns (3,4,5) and so on and so forth.

Finally the len function gives the number of rows in the input file but, what function gives the number of columns in the file?

Thanks.


Append a , to the end of your print statement and it will not print a newline, and then when you exit the for loop add an additional print to move to the next row:

for j in range (0,1):
    ...

    print Px,Py,Pz,
print

Assuming all rows have the same number of columns, you can get the number of columns by using len(row.split()).

Also, you can definitely shorten your code quite a bit, I'm not sure what the purpose of j is, but the following should be equivalent to what you're doing now:

    for j in range (0,1):
        Xp, Yp, Zp = map(float, posn[i].split()[3*j:3*j+3])
        Xc, Yc, Zc = map(float, posn[i+1].split()[3*j:3*j+3])
        ...


You don't need to:

  • use numpy
  • read the whole file in at once
  • know how many columns
  • use awkward comma at end of print statement
  • use list subscripting
  • use math.fabs()
  • explicitly close your file

Try this (untested):

with open("dtest.txt", "r") as posnfile:
    previous = None
    for line in posnfile:
        current = [float(x) for x in line.split()]
        if previous:
            delta = [abs(c - p) for c, p in zip(current, previous)]
            print ' '.join(str(d) for d in delta)
        previous = current


just in case your dtest.txt grows larger and you don't want to redirect your output but rather write to distance.dat, especially, if you want to use numpy. Thank @John for pointing out my mistake in the old code ;-)

import numpy as np
pos = np.genfromtxt("dtest.txt")
dis = np.array([np.abs(pos[j+1] - pos[j]) for j in xrange(len(pos)-1)])
np.savetxt("distance.dat",dis)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜