need help using enumerate ??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric_Dexter@msn.com

    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.appe nd(line.split() )
    parsefile = open('parsefile 1.txt', 'r')
    for line in parsefile:
    parsefile1rows. append(line.spl it())
    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(csoundr ows[row]
    [int(pcol)] + number)
    for row in csoundrows:
    for word in rows:
    outfile.write(r ow)
  • Chris

    #2
    Re: need help using enumerate ??

    On Aug 22, 1:14 pm, "Eric_Dex...@ms n.com" <Eric_Dex...@ms n.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.appe nd(line.split() )
    parsefile = open('parsefile 1.txt', 'r')
    for line in parsefile:
    parsefile1rows. append(line.spl it())
    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(csoundr ows[row]
    [int(pcol)] + number)
    for row in csoundrows:
    for word in rows:
    outfile.write(r ow)
    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('replaceme ntInstructions. txt', 'rb'):
    key = line.strip().sp lit(' ')[0]
    value = line.strip().sp lit(' ')[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().sp lit(' ')
    if record[0] in parseRows:
    record[idx] = parseRows[record[0]]
    outFile.write(' %s\n' % ' '.join(record) )

    inFile.close()
    outFile.close()

    Comment

    • Eric_Dexter@msn.com

      #3
      Re: need help using enumerate ??

      On Aug 22, 7:56 am, Chris <cwi...@gmail.c omwrote:
      On Aug 22, 1:14 pm, "Eric_Dex...@ms n.com" <Eric_Dex...@ms n.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.appe nd(line.split() )
      parsefile = open('parsefile 1.txt', 'r')
      for line in parsefile:
        parsefile1rows. append(line.spl it())
      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(csoundr ows[row]
      [int(pcol)] + number)
      for row in csoundrows:
        for word in rows:
          outfile.write(r ow)
      >
      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('replaceme ntInstructions. txt', 'rb'):
          key = line.strip().sp lit(' ')[0]
          value = line.strip().sp lit(' ')[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().sp lit(' ')
          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..

      Comment

      • bearophileHUGS@lycos.com

        #4
        Re: need help using enumerate ??

        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

        Comment

        Working...