problem with split

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

    #1

    problem with split

    apologies if I annoy and for spacing (google)



    def csdInstrumentLi st(from_file):
    "Returns a list of .csd instruments and any comment lines after the
    instrument"
    infile = open(from_file, 'r')
    temp_number = 0
    for line in infile:
    if 'instr' in line:
    s = re.split(r' +',line,3)
    instr_number = s[1]
    return instr_number

    I am coming pretty close to what I want with variations on theis but I
    cant seem to
    get 3 lines with the split and instr_number[array] = s[1] seems to give
    me an error.
    the data from the line I am trying to split in three would look like
    this

    instr 83 ;comment would be here

    I want comment returned in an array and instr_number returned in an
    array.

  • Eric_Dexter@msn.com

    #2
    Re: problem with split

    p.s. this is the one I need to finish to release the csoundroutines
    library




    Eric_Dexter@msn .com wrote:
    apologies if I annoy and for spacing (google)
    >
    >
    >
    def csdInstrumentLi st(from_file):
    "Returns a list of .csd instruments and any comment lines after the
    instrument"
    infile = open(from_file, 'r')
    temp_number = 0
    for line in infile:
    if 'instr' in line:
    s = re.split(r' +',line,3)
    instr_number = s[1]
    return instr_number
    >
    I am coming pretty close to what I want with variations on theis but I
    cant seem to
    get 3 lines with the split and instr_number[array] = s[1] seems to give
    me an error.
    the data from the line I am trying to split in three would look like
    this
    >
    instr 83 ;comment would be here
    >
    I want comment returned in an array and instr_number returned in an
    array.

    Comment

    • hanumizzle

      #3
      Re: problem with split

      On 6 Oct 2006 21:07:43 -0700, Eric_Dexter@msn .com <Eric_Dexter@ms n.comwrote:
      I want comment returned in an array and instr_number returned in an
      array.
      Let me see if I understand what you want: if there is a line that
      starts with instr (best tested with line.startswith ('instr') :)), you
      want the number and the commen afterwards. I used regexes for this
      purpose. In your case:

      import re

      <snip>

      if line.startswith ('instr'):
      p = re.compile(r'(\ d+)\s+;(.*)$')
      m = p.search(line)

      return (m.group(1), m.group(2))

      I returned a tuple of course; you may wish to change that.

      BTW, thank you -- I just learned how to use re's in Python.

      Comment

      • goyatlah

        #4
        Re: problem with split

        Think you need a regex like this: regex =
        r"\s*instr\s +([0-9]+)\s*(;.*)?"

        Then:
        import re
        test = re.compile(rege x)

        testing is done as follows:
        res = test.match(myst ring)
        if res:
        number = res.group(1) # always a string consisting of decimals
        comment = res.group(2) # string starting with ; or None
        it might be necessary to have the instr part of the regex as follows
        [iI][nN][sS][tT][rR] to handle things like Instr or INSTR.

        Hope this helps

        Dolf

        On 6 Oct 2006 21:07:43 -0700, "Eric_Dexter@ms n.com"
        <Eric_Dexter@ms n.comwrote:
        >apologies if I annoy and for spacing (google)
        >
        >
        >
        >def csdInstrumentLi st(from_file):
        "Returns a list of .csd instruments and any comment lines after the
        >instrument"
        infile = open(from_file, 'r')
        temp_number = 0
        for line in infile:
        if 'instr' in line:
        s = re.split(r' +',line,3)
        instr_number = s[1]
        return instr_number
        >
        >I am coming pretty close to what I want with variations on theis but I
        >cant seem to
        >get 3 lines with the split and instr_number[array] = s[1] seems to give
        >me an error.
        >the data from the line I am trying to split in three would look like
        >this
        >
        instr 83 ;comment would be here
        >
        >I want comment returned in an array and instr_number returned in an
        >array.

        Comment

        • hanumizzle

          #5
          Re: problem with split

          On 10/7/06, goyatlah <goyatlahwrot e:
          Think you need a regex like this: regex =
          r"\s*instr\s +([0-9]+)\s*(;.*)?"
          [0-9] maybe written simply as \d (d for digit)
          Then:
          import re
          test = re.compile(rege x)
          Regexes are usually passed as literals directly to re.compile().
          testing is done as follows:
          res = test.match(myst ring)
          Initial \s* is redundant if you use the search() method.
          if res:
          I forgot that part. :)
          number = res.group(1) # always a string consisting of decimals
          comment = res.group(2) # string starting with ; or None
          it might be necessary to have the instr part of the regex as follows
          [iI][nN][sS][tT][rR] to handle things like Instr or INSTR.
          It is sufficient to use the re.IGNORECASE flag.

          -- Theerasak

          Comment

          • MonkeeSage

            #6
            Re: problem with split



            On Oct 6, 11:33 pm, hanumizzle <hanumiz...@gma il.comwrote:
            import re
            >
            <snip>
            >
            if line.startswith ('instr'):
            p = re.compile(r'(\ d+)\s+;(.*)$')
            m = p.search(line)
            >
            return (m.group(1), m.group(2))
            You probably don't want startswith, in case there are initial spaces in
            the line. Also, since the regexp is single use, you can just use the
            re.search class method, which will compile the regexp implicitly. May
            also want to strip the second grouped match, in case of trailing
            spaces.

            if 'instr' in line:
            m = re.search(r'(\d +)\s+;(.*)$', line)
            if m:
            return (m.group(1), m.group(2).stri p())

            Regards,
            Jordan

            Comment

            • Eric_Dexter@msn.com

              #7
              Re: problem with split

              I was trying something like this

              digits = re.compile("\d" )
              if digits in line
              instr_number = digits.search(l ine)

              because it looked realy cool when I saw it in a recent post... and then
              the same thing
              for just (';') didn't seem to return anything except one line and some
              hex that came from god knows where./... I will see what the other does
              and post the result..

              hanumizzle wrote:
              On 10/7/06, goyatlah <goyatlahwrot e:
              Think you need a regex like this: regex =
              r"\s*instr\s +([0-9]+)\s*(;.*)?"
              >
              [0-9] maybe written simply as \d (d for digit)
              >
              Then:
              import re
              test = re.compile(rege x)
              >
              Regexes are usually passed as literals directly to re.compile().
              >
              testing is done as follows:
              res = test.match(myst ring)
              >
              Initial \s* is redundant if you use the search() method.
              >
              if res:
              >
              I forgot that part. :)
              >
              number = res.group(1) # always a string consisting of decimals
              comment = res.group(2) # string starting with ; or None
              it might be necessary to have the instr part of the regex as follows
              [iI][nN][sS][tT][rR] to handle things like Instr or INSTR.
              >
              It is sufficient to use the re.IGNORECASE flag.
              >
              -- Theerasak

              Comment

              • hanumizzle

                #8
                Re: problem with split

                On 6 Oct 2006 23:09:08 -0700, MonkeeSage <MonkeeSage@gma il.comwrote:
                >
                >
                On Oct 6, 11:33 pm, hanumizzle <hanumiz...@gma il.comwrote:
                import re

                <snip>

                if line.startswith ('instr'):
                p = re.compile(r'(\ d+)\s+;(.*)$')
                m = p.search(line)

                return (m.group(1), m.group(2))
                >
                You probably don't want startswith, in case there are initial spaces in
                the line.
                Pardon me; I am not very familiar with file format in question.
                Also, since the regexp is single use, you can just use the
                re.search class method, which will compile the regexp implicitly.
                Cool.
                May
                also want to strip the second grouped match, in case of trailing
                spaces.
                Cosmetic, but good idea.

                -- Theerasak

                Comment

                • Eric_Dexter@msn.com

                  #9
                  Re: problem with split

                  I think I am very close the return line is tripping me up. (this is
                  the first list that I have tried to program in python)

                  return (s.group[1], s.group[2])

                  Traceback (most recent call last):
                  File "C:\Python24\Li b\site-packages\boa-constructor\tes t of
                  snake\test_of_c soundroutines_l ist.py", line 5, in ?
                  v = csoundroutines. csdInstrumentLi st('bay-at-night.csd')
                  File "C:\Python24\Li b\site-packages\boa-constructor\tes t of
                  snake\csoundrou tines.py", line 43, in csdInstrumentLi st
                  return (s.group[1], s.group[2])
                  TypeError: unsubscriptable object










                  hanumizzle wrote:
                  On 6 Oct 2006 23:09:08 -0700, MonkeeSage <MonkeeSage@gma il.comwrote:


                  On Oct 6, 11:33 pm, hanumizzle <hanumiz...@gma il.comwrote:
                  import re
                  >
                  <snip>
                  >
                  if line.startswith ('instr'):
                  p = re.compile(r'(\ d+)\s+;(.*)$')
                  m = p.search(line)
                  >
                  return (m.group(1), m.group(2))
                  You probably don't want startswith, in case there are initial spaces in
                  the line.
                  >
                  Pardon me; I am not very familiar with file format in question.
                  >
                  Also, since the regexp is single use, you can just use the
                  re.search class method, which will compile the regexp implicitly.
                  >
                  Cool.
                  >
                  May
                  also want to strip the second grouped match, in case of trailing
                  spaces.
                  >
                  Cosmetic, but good idea.
                  >
                  -- Theerasak

                  Comment

                  • Eric_Dexter@msn.com

                    #10
                    Re: problem with split

                    I am not sure if I am having trouble with the test program or the
                    routine.. (I had the brackets in the wrong place on the other)

                    IDLE 1.1.3 ==== No Subprocess ====
                    >>>
                    ['1', 'String pad']
                    >>>
                    I get this but I have at least three lines and the

                    v = []
                    v = csoundroutines. csdInstrumentLi st('bay-at-night.csd')
                    print v




                    Eric_Dexter@msn .com wrote:
                    I think I am very close the return line is tripping me up. (this is
                    the first list that I have tried to program in python)
                    >
                    return (s.group[1], s.group[2])
                    >
                    Traceback (most recent call last):
                    File "C:\Python24\Li b\site-packages\boa-constructor\tes t of
                    snake\test_of_c soundroutines_l ist.py", line 5, in ?
                    v = csoundroutines. csdInstrumentLi st('bay-at-night.csd')
                    File "C:\Python24\Li b\site-packages\boa-constructor\tes t of
                    snake\csoundrou tines.py", line 43, in csdInstrumentLi st
                    return (s.group[1], s.group[2])
                    TypeError: unsubscriptable object
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    hanumizzle wrote:
                    On 6 Oct 2006 23:09:08 -0700, MonkeeSage <MonkeeSage@gma il.comwrote:
                    >
                    >
                    On Oct 6, 11:33 pm, hanumizzle <hanumiz...@gma il.comwrote:
                    import re

                    <snip>

                    if line.startswith ('instr'):
                    p = re.compile(r'(\ d+)\s+;(.*)$')
                    m = p.search(line)

                    return (m.group(1), m.group(2))
                    >
                    You probably don't want startswith, in case there are initial spaces in
                    the line.
                    Pardon me; I am not very familiar with file format in question.
                    Also, since the regexp is single use, you can just use the
                    re.search class method, which will compile the regexp implicitly.
                    Cool.
                    May
                    also want to strip the second grouped match, in case of trailing
                    spaces.
                    Cosmetic, but good idea.

                    -- Theerasak

                    Comment

                    • Steve Holden

                      #11
                      Re: problem with split

                      Eric_Dexter@msn .com wrote:
                      I think I am very close the return line is tripping me up. (this is
                      the first list that I have tried to program in python)
                      >
                      return (s.group[1], s.group[2])
                      >
                      Traceback (most recent call last):
                      File "C:\Python24\Li b\site-packages\boa-constructor\tes t of
                      snake\test_of_c soundroutines_l ist.py", line 5, in ?
                      v = csoundroutines. csdInstrumentLi st('bay-at-night.csd')
                      File "C:\Python24\Li b\site-packages\boa-constructor\tes t of
                      snake\csoundrou tines.py", line 43, in csdInstrumentLi st
                      return (s.group[1], s.group[2])
                      TypeError: unsubscriptable object
                      >
                      ..group() is a *method of the patch object not a data attribute, so you
                      have to *call* it, not treat it like a list or dict. Try something like

                      return (s.group(1), s.group(2))

                      regards
                      Steve
                      --
                      Steve Holden +44 150 684 7255 +1 800 494 3119
                      Holden Web LLC/Ltd http://www.holdenweb.com
                      Skype: holdenweb http://holdenweb.blogspot.com
                      Recent Ramblings http://del.icio.us/steve.holden

                      Comment

                      • Bruno Desthuilliers

                        #12
                        Re: problem with split

                        hanumizzle wrote:
                        (snip)
                        Regexes are usually passed as literals directly to re.compile().
                        For which definition of "usually" ?

                        --
                        bruno desthuilliers
                        python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                        p in 'onurb@xiludom. gro'.split('@')])"

                        Comment

                        • Theerasak Photha

                          #13
                          Re: problem with split

                          On 10/9/06, Bruno Desthuilliers <onurb@xiludom. growrote:
                          hanumizzle wrote:
                          (snip)
                          Regexes are usually passed as literals directly to re.compile().
                          >
                          For which definition of "usually" ?
                          >From definition of personal experience: all of the code I've written
                          or seen that used small regexes in such a context as this wrote them
                          out literally. Putting such a short string, used only once, in
                          variable form seems a little verbose unless you have reuse in mind.

                          IOW, when doing simple arithmetic on paper in an informal context, you
                          "usually" don't assign the operands to variables before performing the
                          operations in question.

                          -- Theerasak

                          Comment

                          Working...