472,364 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 software developers and data experts.

need help using enumerate ??

I am trying to take some data in file that looks like this

command colnum_1 columnum_2

and look for the command and then cange the value in the collum(word)
number indicated. I am under
the impression I need enumerate but I am not sure what to do with it
any help would be nice.

import sys

parse1filerows = []
csoundrows = []

filename = sys.argv[0]
number = sys.argv[1]
outfile = open('test.sco','w')

infile = open(filename, 'r')
for line in infile:
csoundrows.append(line.split())
parsefile = open('parsefile1.txt', 'r')
for line in parsefile:
parsefile1rows.append(line.split())
for row in csoundrows:
for prow in parsefile1rows:
test = 0
if parsefile1[prow][0] in csoundrow[row]:
for pcol in parsefile1[prow]:
if test == 1:
csoundrows[row][int(pcol)] = str(int(csoundrows[row]
[int(pcol)] + number)
for row in csoundrows:
for word in rows:
outfile.write(row)
Aug 22 '08 #1
3 1746
On Aug 22, 1:14 pm, "Eric_Dex...@msn.com" <Eric_Dex...@msn.comwrote:
I am trying to take some data in file that looks like this

command colnum_1 columnum_2

and look for the command and then cange the value in the collum(word)
number indicated. I am under
the impression I need enumerate but I am not sure what to do with it
any help would be nice.

import sys

parse1filerows = []
csoundrows = []

filename = sys.argv[0]
number = sys.argv[1]
outfile = open('test.sco','w')

infile = open(filename, 'r')
for line in infile:
csoundrows.append(line.split())
parsefile = open('parsefile1.txt', 'r')
for line in parsefile:
parsefile1rows.append(line.split())
for row in csoundrows:
for prow in parsefile1rows:
test = 0
if parsefile1[prow][0] in csoundrow[row]:
for pcol in parsefile1[prow]:
if test == 1:
csoundrows[row][int(pcol)] = str(int(csoundrows[row]
[int(pcol)] + number)
for row in csoundrows:
for word in rows:
outfile.write(row)
Rather confusing code there and non-functional.

You never close your file handles, when finished with a file use the
..close() method
sys.argv[0] <-- the first element is the name of your .py file and
not
the first argument you supply.
When iterating over a list like csoundrows you don't need to do
for row in csoundrows:
if ... in csoundrow[row]: # This will try to use 'row' as an
index

but rather

if ... in row:

Now, this is how I intepretted your question.

from sys import argv, exit

if len(argv) != 3:
"""Ensure the correct number of arguments are supplied"""
exit('Incorrect number of arguments.')

try:
"""Checks if the Input file exists and exits if open fails."""
inFile = open(argv[1], 'rb')
except IOError:
exit('Input file does not exist.')

if not argv[2].isdigit():
"""Argument #2 needs to be a number"""
exit('Column number is not numerical.')

idx = int(argv[2])
outFile = open('test.sco', 'wb')

"""Assuming your data in the parse file was a set of key, value pairs
to be used for replacement in the input file. Just splitting on
the
basic space and assigning the first element as the key and the rest
of
the string as the value to be used for replacement.
"""
replaceData = {}
for line in open('replacementInstructions.txt', 'rb'):
key = line.strip().split(' ')[0]
value = line.strip().split(' ')[1:]
replaceData[key] = value

"""Iterate over your input file, split the line into it's component
parts
and then lookup if the first element 'command' is contained in the
replacement data and if so change the data.
If you want all input to be saved into your output file, just
dedent
the 'outFile.write' line by one level and then all data will be
saved.
"""
for line in inFile:
record = line.strip().split(' ')
if record[0] in parseRows:
record[idx] = parseRows[record[0]]
outFile.write('%s\n' % ' '.join(record) )

inFile.close()
outFile.close()
Aug 22 '08 #2
On Aug 22, 7:56*am, Chris <cwi...@gmail.comwrote:
On Aug 22, 1:14 pm, "Eric_Dex...@msn.com" <Eric_Dex...@msn.comwrote:


I am trying to take some data in * file that looks like this
command colnum_1 columnum_2
and look for the command and then cange the value in the collum(word)
number indicated. *I am under
the impression I need enumerate but I am not sure what to do with it
any help would be nice.
import sys
parse1filerows = []
csoundrows = []
filename = sys.argv[0]
number = sys.argv[1]
outfile = open('test.sco','w')
infile = open(filename, 'r')
for line in infile:
* csoundrows.append(line.split())
parsefile = open('parsefile1.txt', 'r')
for line in parsefile:
* parsefile1rows.append(line.split())
for row in csoundrows:
* for prow in parsefile1rows:
* * test = 0
* * if parsefile1[prow][0] in csoundrow[row]:
* * * for pcol in parsefile1[prow]:
* * * * if test == 1:
* * * * * csoundrows[row][int(pcol)] = str(int(csoundrows[row]
[int(pcol)] + number)
for row in csoundrows:
* for word in rows:
* * outfile.write(row)

Rather confusing code there and non-functional.

You never close your file handles, when finished with a file use the
.close() method
sys.argv[0] *<-- the first element is the name of your .py file and
not
* * * * * * * * *the first argument you supply.
When iterating over a list like csoundrows you don't need to do
for row in csoundrows:
* * if ... in csoundrow[row]: *# This will try to use 'row' as an
index

* * but rather

* * if ... in row:

Now, this is how I intepretted your question.

from sys import argv, exit

if len(argv) != 3:
* * """Ensure the correct number of arguments are supplied"""
* * exit('Incorrect number of arguments.')

try:
* * """Checks if the Input file exists and exits if open fails."""
* * inFile = open(argv[1], 'rb')
except IOError:
* * exit('Input file does not exist.')

if not argv[2].isdigit():
* * """Argument #2 needs to be a number"""
* * exit('Column number is not numerical.')
There is a number to be added to the text in that column... That is a
slight edit though it still needs to be a number
>
idx = int(argv[2])
outFile = open('test.sco', 'wb')

"""Assuming your data in the parse file was a set of key, value pairs
* *to be used for replacement in the input file. *Just splitting on
the
* *basic space and assigning the first element as the key and the rest
of
* *the string as the value to be used for replacement.
"""
replaceData = {}
for line in open('replacementInstructions.txt', 'rb'):
* * key = line.strip().split(' ')[0]
* * value = line.strip().split(' ')[1:]
* * replaceData[key] = value

"""Iterate over your input file, split the line into it's component
parts
* *and then lookup if the first element 'command' is contained in the
* *replacement data and if so change the data.
* *If you want all input to be saved into your output file, just
dedent
* *the 'outFile.write' line by one level and then all data will be
saved.
"""
for line in inFile:
* * record = line.strip().split(' ')
* * if record[0] in parseRows:
* * * * record[idx] = parseRows[record[0]]
* * * * outFile.write('%s\n' % ' '.join(record) )

inFile.close()
outFile.close()- Hide quoted text -

- Show quoted text -
I need to first find if the csound command is in the line and then go
to the n'th word (a number) and add another number to it. I may need
to do that for up to 5 values in the line (maybe more for cound
commands that haven't been thought up yet). I then convert the data
back to text. These all point to ftable's when I renumber them i.e.
f1 to f3 I have to renumber them in the program file as well. .orc
and .sco.

If I load in a 5 from the file it means I have to load the data from
the 5th column and add a number to it and save it back as text. I
have found that the .close is automatically called and sometimes you
can try to close a closed file causing an error.. So I let python do
it.. instead of coding this in awk I try to emulate grid code that I
have.. I will study this though and it does have code that is useful
and helpful.

this is the easy command list I will try to do lists with var number
of commands by counting the number of words in a line ,'s exc..
Aug 22 '08 #3
Chris:
"""Iterate over your input file, split the line into it's component
parts
and then lookup if the first element 'command' is contained in the
replacement data and if so change the data.
If you want all input to be saved into your output file, just
dedent
the 'outFile.write' line by one level and then all data will be
saved.
"""
I'd like to have multi-line comments in Python. Your usage of multi-
lines to simulate multi-line comments may be discouraged...

Bye,
bearophile
Aug 22 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: george young | last post by:
For each run of my app, I have a known set of (<100) wafer names. Names are sometimes simply integers, sometimes a short string, and sometimes a short string followed by an integer, e.g.: 5, 6,...
45
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
5
by: nephish | last post by:
hey there, i need to be able to get the index for an item in a list. the list is a list of lines read from a text file. like this: file = open("/home/somefile.text", "r") lines =...
6
by: Gregory Petrosyan | last post by:
Hello! I have a question for the developer of enumerate(). Consider the following code: for x,y in coords(dots): print x, y When I want to iterate over enumerated sequence I expect this to...
21
by: James Stroud | last post by:
I think that it would be handy for enumerate to behave as such: def enumerate(itrbl, start=0, step=1): i = start for it in itrbl: yield (i, it) i += step This allows much more flexibility...
13
by: David | last post by:
Hi all, I have a singleton ienumerable that collects data from a database. I have listed the code below. It has the usual methods of current, move and reset. I need to go to a certain position...
3
by: ssghill | last post by:
Okay all i'm new to the asp portion but fairly good with vbscript. My problem is that I am trying to us an asp page to enumerate an ou. The problem is when I us localhoston the intranet web...
13
by: Kurda Yon | last post by:
Hi, I found one example which defines the addition of two vectors as a method of a class. It looks like that: class Vector: def __add__(self, other): data = for j in range(len(self.data)):...
2
by: cloftis | last post by:
Using VS2003, VB and MSHTML, Using an HTMLSpanElement I want to enumerate the attributes of a SPAN tag. 1 'For testing sake 2 Dim strMarkup as String = "<span attr1='somevalue'...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.