python read and write column data to file
I have a file like this and I want to get rid of the first column, then move the fifth column to first and add four more new columns:
Input file:
24 -2.69607 -2.96753 0.496292 0.49663
42 -1.9176 -2.44219 0.440086 0.440357
6 0.22615 -2.52973 0.515513 0.515875
Output file:
0.49663 -2.69607 -2.96753 0.496292 1.0 1.0 0.0 0.0
0.440357 -1.9176 -2.44219 0.440086 1.0 1.0 0.0 0.0
0.515875 0.22615 -2.52973 0.515513 1.0 1.0 0.0 0.0
Python code to do this:
#!/usr/bin/python
#command line: python posCreate.py spheres.text output.pos
import sys
from numpy import*
#for arg in sys.argv:
# print arg
print 'Input file:'+ sys.argv[1]
print 'Output file:'+ sys.argv[2]
xX = []
xY = []
xZ = []
xRadius = []
xrefReal = []
xrefImg = []
xchiralReal = []
xchiralImg = []
inputFile = open(sys.argv[1], "r+")
for line in inputFile:
line = line.split()
xx = line[1]
xy = line[2]
xz = line[3]
xradius = line[4]
xX.append(float(xx))
xY.append(float(xy))
xZ.append(float(xz))
xRadius.append(float(xradius))
for i in range(len(xX)+1):
xrefReal = ones (i)
xrefImg = ones (i)
xchiralReal = zeros(i)
xchiralImg = zeros(i)
#Output a new file formatted for MSTS *.inp format
outputFile = open(sys.argv[2], "r+")
for i in range(len(xX)):
print >>outputFile, xRadius[i], xX[i], xY[i], xZ[i], xrefReal[i], xrefImg[i], xchiralReal[i], xchiralImg[i]
#above line prints lines in the output file
outputFile.close()
print 'Successfully created .pos file from MSTM output file!'
Last modified: 2017-07-21