need help using enumerate ??

Chris cwitts at gmail.com
Fri Aug 22 08:56:42 EDT 2008


On Aug 22, 1:14 pm, "Eric_Dex... at msn.com" <Eric_Dex... at msn.com> wrote:
> 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()



More information about the Python-list mailing list