Right now, I have this python script that lets me choose which column of a text file that I can zero out. Them column nubmer is stored as the variable 'elem'. I also was given a sample of taking in command line arguements. I was wondering if anyone could help me intergrate this into the code so that from the command line I can input a number and have the script zero out the column.
beginning text file:
#Number of Bits
12
#Data
0 0 0 0 0 0 0 0 0 0 0 0
12 5 3 4 6 4 5 4 7 5 5 10
24 9 7 7 13 7 9 9 14 10 10 20
text file after script has been run:
#Number of Bits
12
#Data
0 0 0 0 0 0 0 0 0 0 0 0
0 5 3 4 6 4 5 4 7 5 5 10
0 9 7 7 13 7 9 9 14 10 10 20
and this is what it does:
example of the command line
beginning text file:
#Number of Bits
12
#Data
0 0 0 0 0 0 0 0 0 0 0 0
12 5 3 4 6 4 5 4 7 5 5 10
24 9 7 7 13 7 9 9 14 10 10 20
text file after script has been run:
#Number of Bits
12
#Data
0 0 0 0 0 0 0 0 0 0 0 0
0 5 3 4 6 4 5 4 7 5 5 10
0 9 7 7 13 7 9 9 14 10 10 20
and this is what it does:
Code:
def nthzero(dataList, nth, n):
'''
Replace the nth element of each list in the data list with 'n'
'''
for item in dataList:
item[nth] = n
return dataList
fn = '/outfile.txt'
f = open(fn)
s = f.readline()
prefix = s
while s.strip() != '#Data':
s = f.readline()
prefix += s
lineList = [line.strip().split() for line in f.readlines()]
for line in f.readlines():
lineList.append(line.strip().split())
f.close()
elem = 0
repl = '0'
lineList = nthzero(lineList, elem, repl)
fn1 = '/outfile.txt'
f = open(fn1, 'w')
outList = []
for line in lineList:
outList.append(' '.join(line))
f.write('%s%s' % (prefix, '\n'.join(outList)))
f.close()
Code:
/usr/bin/python hello.py \"1, 2, 4, 6, 9\""
hello.py
import sys
names = sys.argv[1].split(', ')
list = []
for name in names:
list.append(name)
print list
#print "Hello " + sys.argv[1]
output:
Hello [1, 2, 4, 6, 9]
Comment