need help using enumerate ??

Discussion in 'Python' started by Eric_Dexter@msn.com, Aug 22, 2008.

  1. Guest

    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, 2008
    #1
    1. Advertising

  2. Chris Guest

    On Aug 22, 1:14 pm, "" <> 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()
     
    Chris, Aug 22, 2008
    #2
    1. Advertising

  3. Guest

    On Aug 22, 7:56 am, Chris <> wrote:
    > On Aug 22, 1:14 pm, "" <> 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.')


    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, 2008
    #3
  4. Guest

    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, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. poi

    Enumerate Roles?

    poi, Nov 14, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    508
    Oliver
    Nov 15, 2003
  2. shiv
    Replies:
    3
    Views:
    6,798
    S. Justin Gengo
    Dec 3, 2003
  3. localhost

    Enumerate Control Attributes?

    localhost, Dec 16, 2003, in forum: ASP .Net
    Replies:
    7
    Views:
    619
    localhost
    Dec 22, 2003
  4. Arsen Vladimirskiy

    How to enumerate sessions?

    Arsen Vladimirskiy, Jan 9, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    3,436
    David Browne
    Jan 9, 2004
  5. arnie
    Replies:
    0
    Views:
    500
    arnie
    Jul 25, 2007
Loading...

Share This Page