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: -
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()
-
example of the command line -
/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]
-
11 2708
This code is in Python 2.0.
/usr/bin/python hello.py 1 2 4 6 9
I have a couple of examples that I have been distributing one is in tracker8.py available in my dex tracker project (url at bottom). What I did was use a main function. The code you are writing looks like it would be usefull for what I am doing and maybe something that would allow a series of numbers from a file into the column you are zeroing out might be a good contribution to my project :) ... http://www.stormpages.com/edexter/csound.html
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: -
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()
-
example of the command line -
/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]
-
I haven't tried this yet but another way to write your program may be to use vim as a com object (with pywin or maybe through wxpython). There was a recent post on the vim board that describes a way to insert a column...
http://groups.google.com/group/vim-experiment/browse_thread/thread/447a01d7014be5c9
This has all been pretty tempting but I have been coding other stuff
bvdet 2,851
Expert Mod 2GB
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: -
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()
-
example of the command line -
/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]
-
At the command line: python code.py 1 2 4 6 9
Assign the following to variable ' elem': - elem = [int(i) for i in sys.argv[1:]]
Following is the code I used: - import sys
-
-
def nthzero(dataList, nthList, n):
-
'''
-
Replace the nth element of each list in dataList with 'n'
-
'''
-
for nth in nthList:
-
for item in dataList:
-
try:
-
item[nth] = n
-
except IndexError, e:
-
pass
-
return dataList
-
-
if __name__ == '__main__':
-
-
fn = 'datain.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()]
-
-
f.close()
-
#elem = [1,2,3,4,8,9,15]
-
-
# Command Line
-
# C:\Python23>python nthzero.py 1 2 3 4 8 9 15
-
elem = [int(i) for i in sys.argv[1:]]
-
-
repl = '0'
-
lineList = nthzero(lineList, elem, repl)
-
-
fn1 = 'dataout.txt'
-
f = open(fn1, 'w')
-
-
outList = []
-
-
for line in lineList:
-
outList.append(' '.join(line))
-
-
f.write('%s%s' % (prefix, '\n'.join(outList)))
-
f.close()
this script takes in numbers one at a time eventhough they are listed together. Is there a way that I can input an entire list, i.e. 0 1 2 3 4 5, from the command line and have the script zero them out? Kinda like the example: -
import sys
-
names = sys.argv[1].split(', ')
-
list = []
-
for name in names:
-
list.append(name)
-
print list
-
#print "Hello " + sys.argv[1]
-
this script takes in numbers one at a time eventhough they are listed together. Is there a way that I can input an entire list, i.e. 0 1 2 3 4 5, from the command line and have the script zero them out? Kinda like the example: -
import sys
-
names = sys.argv[1].split(', ')
-
list = []
-
for name in names:
-
list.append(name)
-
print list
-
#print "Hello " + sys.argv[1]
-
You can put quotes around your command-line argument: "1, 2, 3" (I think).
As I said before, I am a noob at python and am trying to understand what is going on. I was wondering if you help me understand what a couple of the lines of the script are doing. -
lineList = [line.strip().split() for line in f.readlines()]
-
how does this script work?
As I said before, I am a noob at python and am trying to understand what is going on. I was wondering if you help me understand what a couple of the lines of the script are doing. -
lineList = [line.strip().split() for line in f.readlines()]
-
That's called a list comprehension. In order to understand it, let's break it down: - tempList = f.readlines()
-
lineList = []
-
for line in tempList:
-
newLine = line.strip()
-
print newLine
-
wordList = newLine.split()
-
print wordList
-
lineList.append(wordList)
-
print lineList
Does that help?
it helps a little more. Can you add some comments to the code to help me get a better a better understanding of which sections do what?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Hari |
last post by:
can i have command line arguments in VS.NET applicatio?
if yes how?
Can i have some code snippets of the above functionality?
I know we can acjieve this in console application form
command...
|
by: SunRise |
last post by:
Hi
I am creating a C Program , to extract
only-Printable-characters from a file ( any type of file) and display
them.
OS: Windows-XP
Ple help me to fix the Errors & Warnings and explain...
|
by: amirmira |
last post by:
I would like to set command line arguments to a service at install time. I
need to do this because I need to get information from different registry
locations depending on my command line argument....
|
by: Hari |
last post by:
can i have command line arguments in VS.NET applicatio?
if yes how?
Can i have some code snippets of the above functionality?
I know we can acjieve this in console application form
command...
|
by: Jwe |
last post by:
Hi,
I've written a program which has both a command line interface
and Windows form interface, however it isn't quite working
correctly.
When run from command line with no arguments it should...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |