Why isn't SPLIT splitting my strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ronrsr

    #1

    Why isn't SPLIT splitting my strings

    I'm trying to break up the result tuple into keyword phrases. The
    keyword phrases are separated by a ; -- the split function is not
    working the way I believe it should be. Can anyone see what I"m doing
    wrong?

    bests,

    -rsr-



    print "newstring =", str(result[i][0])
    print "just split", str(result[i][0]).split('; ()[]"')
    zd.append(str(r esult[i][0]).split('; ()[]"'))


    result= (('Agricultural subsidies; Foreign aid',), ('Agriculture;
    Sustainable Agriculture - Support; Organic Agriculture; Pesticides, US,
    Childhood Development, Birth Defects; Toxic Chemicals',),
    ('Antibiotics, Animals',), ('Agricultural Subsidies, Global Trade',),
    ('Agricultural Subsidies',), ('Biodiversity' ,), ('Citizen Activism',),
    ('Community Gardens',), ('Cooperatives' ,), ('Dieting',), ('Agriculture,
    Cotton',), ('Agriculture, Global Trade',), ('Pesticides, Monsanto',),
    ('Agriculture, Seed',), ('Coffee, Hunger',), ('Pollution, Water,
    Feedlots',), ('Food Prices',), ('Agriculture, Workers',), ('Animal
    Feed, Corn, Pesticides',), ('Aquaculture', ), ('Chemical Warfare',),
    ('Compost',), ('Debt',), ('Consumerism', ), ('Fear',), ('Pesticides, US,
    Childhood Development, Birth Defects',), ('Corporate Reform,
    Personhood (Dem. Book)',), ('Corporate Reform, Personhood, Farming
    (Dem. Book)',), ('Crime Rates, Legislation, Education',), ('Debt,
    Credit Cards',), ('Democracy',), ('Population, World',), ('Income',),
    ('Democracy, Corporate



    partial results:

    string a = Agricultural subsidies; Foreign aid
    newstring = Agricultural subsidies; Foreign aid
    just split ['Agricultural subsidies; Foreign aid']
    newstring = Agriculture; Sustainable Agriculture - Support; Organic
    Agriculture; Pesticides, US, Childhood Development, Birth Defects;
    Toxic Chemicals
    just split ['Agriculture; Sustainable Agriculture - Support; Organic
    Agriculture; Pesticides, US, Childhood Development, Birth Defects;
    Toxic Chemicals']
    newstring = Antibiotics, Animals
    just split ['Antibiotics, Animals']

  • Nick Vatamaniuc

    #2
    Re: Why isn't SPLIT splitting my strings

    The regular string split does not take a _class_ of characters as the
    separator, it only takes one separator. Your example suggests that you
    expect that _any_ of the characters "; ()[]" could be a separator.
    If you want to use a regular expression as a list of separators, then
    you need to use the split function from the regular expression module
    (re):


    Hope this helps,
    Nick V.


    ronrsr wrote:
    I'm trying to break up the result tuple into keyword phrases. The
    keyword phrases are separated by a ; -- the split function is not
    working the way I believe it should be. Can anyone see what I"m doing
    wrong?
    >
    bests,
    >
    -rsr-
    >
    >
    >
    print "newstring =", str(result[i][0])
    print "just split", str(result[i][0]).split('; ()[]"')
    zd.append(str(r esult[i][0]).split('; ()[]"'))
    >
    >
    result= (('Agricultural subsidies; Foreign aid',), ('Agriculture;
    Sustainable Agriculture - Support; Organic Agriculture; Pesticides, US,
    Childhood Development, Birth Defects; Toxic Chemicals',),
    ('Antibiotics, Animals',), ('Agricultural Subsidies, Global Trade',),
    ('Agricultural Subsidies',), ('Biodiversity' ,), ('Citizen Activism',),
    ('Community Gardens',), ('Cooperatives' ,), ('Dieting',), ('Agriculture,
    Cotton',), ('Agriculture, Global Trade',), ('Pesticides, Monsanto',),
    ('Agriculture, Seed',), ('Coffee, Hunger',), ('Pollution, Water,
    Feedlots',), ('Food Prices',), ('Agriculture, Workers',), ('Animal
    Feed, Corn, Pesticides',), ('Aquaculture', ), ('Chemical Warfare',),
    ('Compost',), ('Debt',), ('Consumerism', ), ('Fear',), ('Pesticides, US,
    Childhood Development, Birth Defects',), ('Corporate Reform,
    Personhood (Dem. Book)',), ('Corporate Reform, Personhood, Farming
    (Dem. Book)',), ('Crime Rates, Legislation, Education',), ('Debt,
    Credit Cards',), ('Democracy',), ('Population, World',), ('Income',),
    ('Democracy, Corporate
    >
    >
    >
    partial results:
    >
    string a = Agricultural subsidies; Foreign aid
    newstring = Agricultural subsidies; Foreign aid
    just split ['Agricultural subsidies; Foreign aid']
    newstring = Agriculture; Sustainable Agriculture - Support; Organic
    Agriculture; Pesticides, US, Childhood Development, Birth Defects;
    Toxic Chemicals
    just split ['Agriculture; Sustainable Agriculture - Support; Organic
    Agriculture; Pesticides, US, Childhood Development, Birth Defects;
    Toxic Chemicals']
    newstring = Antibiotics, Animals
    just split ['Antibiotics, Animals']

    Comment

    • Duncan Booth

      #3
      Re: Why isn't SPLIT splitting my strings

      "ronrsr" <ronrsr@gmail.c omwrote:
      I'm trying to break up the result tuple into keyword phrases. The
      keyword phrases are separated by a ; -- the split function is not
      working the way I believe it should be. Can anyone see what I"m doing
      wrong?
      I think your example boils down to:
      >>help(str.spli t)
      Help on method_descript or:

      split(...)
      S.split([sep [,maxsplit]]) -list of strings

      Return a list of the words in the string S, using sep as the
      delimiter string. If maxsplit is given, at most maxsplit
      splits are done. If sep is not specified or is None, any
      whitespace string is a separator.
      >>s = 'Antibiotics, Animals'
      >>s.split('; ()[]"')
      ['Antibiotics, Animals']

      This is what I expect. The separator string '; ()[]"' does not occur
      anywhere in the input string, so there is nowhere to split it.

      If you want to split on multiple single characters, then either use
      re.split and filter out the separators, or use string.translat e or
      str.replace to replace all of the separators with the same character and
      split on that. e.g.
      >>def mysplit(s):
      s = s.replace(' ', ';')
      s = s.replace('(', ';')
      s = s.replace(')', ';')
      s = s.replace('[', ';')
      s = s.replace(']', ';')
      s = s.replace('"', ';')
      return s.split(';')
      >>mysplit(s)
      ['Antibiotics,', 'Animals']

      which still looks a bit weird as output, but you never actually said what
      output you wanted.

      Comment

      • Fredrik Lundh

        #4
        Re: Why isn't SPLIT splitting my strings

        ronrsr wrote:
        I'm trying to break up the result tuple into keyword phrases. The
        keyword phrases are separated by a ; -- the split function is not
        working the way I believe it should be.
        >>help(str.spli t)
        split(...)
        S.split([sep [,maxsplit]]) -list of strings

        Return a list of the words in the string S, using sep as the
        delimiter string.

        note that it says *delimiter string*, not *list of characters that I
        want it to split on*.

        </F>

        Comment

        • Paul McGuire

          #5
          Re: Why isn't SPLIT splitting my strings

          "ronrsr" <ronrsr@gmail.c omwrote in message
          news:1163931285 .390782.130500@ h54g2000cwb.goo glegroups.com.. .
          I'm trying to break up the result tuple into keyword phrases. The
          keyword phrases are separated by a ; -- the split function is not
          working the way I believe it should be. Can anyone see what I"m doing
          wrong?
          >
          bests,
          >
          -rsr-
          >
          >
          >
          print "newstring =", str(result[i][0])
          print "just split", str(result[i][0]).split('; ()[]"')
          zd.append(str(r esult[i][0]).split('; ()[]"'))
          >
          >
          result= (('Agricultural subsidies; Foreign aid',), ('Agriculture;
          Sustainable Agriculture - Support; Organic Agriculture; Pesticides, US,
          Childhood Development, Birth Defects; Toxic Chemicals',),
          ('Antibiotics, Animals',), ('Agricultural Subsidies, Global Trade',),
          ('Agricultural Subsidies',), ('Biodiversity' ,), ('Citizen Activism',),
          ('Community Gardens',), ('Cooperatives' ,), ('Dieting',), ('Agriculture,
          Cotton',), ('Agriculture, Global Trade',), ('Pesticides, Monsanto',),
          ('Agriculture, Seed',), ('Coffee, Hunger',), ('Pollution, Water,
          Feedlots',), ('Food Prices',), ('Agriculture, Workers',), ('Animal
          Feed, Corn, Pesticides',), ('Aquaculture', ), ('Chemical Warfare',),
          ('Compost',), ('Debt',), ('Consumerism', ), ('Fear',), ('Pesticides, US,
          Childhood Development, Birth Defects',), ('Corporate Reform,
          Personhood (Dem. Book)',), ('Corporate Reform, Personhood, Farming
          (Dem. Book)',), ('Crime Rates, Legislation, Education',), ('Debt,
          Credit Cards',), ('Democracy',), ('Population, World',), ('Income',),
          ('Democracy, Corporate
          >
          >
          There are no ()'s or []'s in any of your tuple values, these are just
          punctuation to show you the nested tuple structure of your db query results.
          You say ';' is your keyword delimiter, so just split on ';'.

          Also split('; ()[]') does not split on any one of the listed characters, but
          only on the complete string '; ()[]', which occurs nowhere in your data.

          -- Paul


          # for each result in the tuple of tuples, split on ';', and then print the
          results separated by " : " to show that we really did separate the results

          for t in result:
          print " : ".join(t[0].split(';'))

          Gives:
          Agricultural subsidies : Foreign aid
          Agriculture : Sustainable Agriculture - Support : Organic Agriculture :
          Pesticides, US,Childhood Development, Birth Defects : Toxic Chemicals
          Antibiotics, Animals
          Agricultural Subsidies, Global Trade
          Agricultural Subsidies
          Biodiversity
          Citizen Activism
          Community Gardens
          Cooperatives
          Dieting
          Agriculture,Cot ton
          Agriculture, Global Trade
          Pesticides, Monsanto
          Agriculture, Seed
          Coffee, Hunger
          Pollution, Water,Feedlots
          Food Prices
          Agriculture, Workers
          AnimalFeed, Corn, Pesticides
          Aquaculture
          Chemical Warfare
          Compost
          Debt
          Consumerism
          Fear
          Pesticides, US,Childhood Development, Birth Defects
          Corporate Reform,Personho od (Dem. Book)
          Corporate Reform, Personhood, Farming(Dem. Book)
          Crime Rates, Legislation, Education
          Debt,Credit Cards
          Democracy
          Population, World
          Income


          Comment

          • John Henry

            #6
            Re: Why isn't SPLIT splitting my strings

            Meaning:

            import re

            newString= re.split('[; ()\[\]", result)



            Nick Vatamaniuc wrote:
            The regular string split does not take a _class_ of characters as the
            separator, it only takes one separator. Your example suggests that you
            expect that _any_ of the characters "; ()[]" could be a separator.
            If you want to use a regular expression as a list of separators, then
            you need to use the split function from the regular expression module
            (re):

            >
            Hope this helps,
            Nick V.
            >
            >
            ronrsr wrote:
            I'm trying to break up the result tuple into keyword phrases. The
            keyword phrases are separated by a ; -- the split function is not
            working the way I believe it should be. Can anyone see what I"m doing
            wrong?

            bests,

            -rsr-



            print "newstring =", str(result[i][0])
            print "just split", str(result[i][0]).split('; ()[]"')
            zd.append(str(r esult[i][0]).split('; ()[]"'))


            result= (('Agricultural subsidies; Foreign aid',), ('Agriculture;
            Sustainable Agriculture - Support; Organic Agriculture; Pesticides, US,
            Childhood Development, Birth Defects; Toxic Chemicals',),
            ('Antibiotics, Animals',), ('Agricultural Subsidies, Global Trade',),
            ('Agricultural Subsidies',), ('Biodiversity' ,), ('Citizen Activism',),
            ('Community Gardens',), ('Cooperatives' ,), ('Dieting',), ('Agriculture,
            Cotton',), ('Agriculture, Global Trade',), ('Pesticides, Monsanto',),
            ('Agriculture, Seed',), ('Coffee, Hunger',), ('Pollution, Water,
            Feedlots',), ('Food Prices',), ('Agriculture, Workers',), ('Animal
            Feed, Corn, Pesticides',), ('Aquaculture', ), ('Chemical Warfare',),
            ('Compost',), ('Debt',), ('Consumerism', ), ('Fear',), ('Pesticides, US,
            Childhood Development, Birth Defects',), ('Corporate Reform,
            Personhood (Dem. Book)',), ('Corporate Reform, Personhood, Farming
            (Dem. Book)',), ('Crime Rates, Legislation, Education',), ('Debt,
            Credit Cards',), ('Democracy',), ('Population, World',), ('Income',),
            ('Democracy, Corporate



            partial results:

            string a = Agricultural subsidies; Foreign aid
            newstring = Agricultural subsidies; Foreign aid
            just split ['Agricultural subsidies; Foreign aid']
            newstring = Agriculture; Sustainable Agriculture - Support; Organic
            Agriculture; Pesticides, US, Childhood Development, Birth Defects;
            Toxic Chemicals
            just split ['Agriculture; Sustainable Agriculture - Support; Organic
            Agriculture; Pesticides, US, Childhood Development, Birth Defects;
            Toxic Chemicals']
            newstring = Antibiotics, Animals
            just split ['Antibiotics, Animals']

            Comment

            • John Henry

              #7
              Re: Why isn't SPLIT splitting my strings

              Oops!

              newString= re.split("[; ()\[\]", result)

              John Henry wrote:
              Meaning:
              >
              import re
              >
              newString= re.split('[; ()\[\]", result)
              >
              >
              >
              Nick Vatamaniuc wrote:
              The regular string split does not take a _class_ of characters as the
              separator, it only takes one separator. Your example suggests that you
              expect that _any_ of the characters "; ()[]" could be a separator.
              If you want to use a regular expression as a list of separators, then
              you need to use the split function from the regular expression module
              (re):


              Hope this helps,
              Nick V.


              ronrsr wrote:
              I'm trying to break up the result tuple into keyword phrases. The
              keyword phrases are separated by a ; -- the split function is not
              working the way I believe it should be. Can anyone see what I"m doing
              wrong?
              >
              bests,
              >
              -rsr-
              >
              >
              >
              print "newstring =", str(result[i][0])
              print "just split", str(result[i][0]).split('; ()[]"')
              zd.append(str(r esult[i][0]).split('; ()[]"'))
              >
              >
              result= (('Agricultural subsidies; Foreign aid',), ('Agriculture;
              Sustainable Agriculture - Support; Organic Agriculture; Pesticides, US,
              Childhood Development, Birth Defects; Toxic Chemicals',),
              ('Antibiotics, Animals',), ('Agricultural Subsidies, Global Trade',),
              ('Agricultural Subsidies',), ('Biodiversity' ,), ('Citizen Activism',),
              ('Community Gardens',), ('Cooperatives' ,), ('Dieting',), ('Agriculture,
              Cotton',), ('Agriculture, Global Trade',), ('Pesticides, Monsanto',),
              ('Agriculture, Seed',), ('Coffee, Hunger',), ('Pollution, Water,
              Feedlots',), ('Food Prices',), ('Agriculture, Workers',), ('Animal
              Feed, Corn, Pesticides',), ('Aquaculture', ), ('Chemical Warfare',),
              ('Compost',), ('Debt',), ('Consumerism', ), ('Fear',), ('Pesticides, US,
              Childhood Development, Birth Defects',), ('Corporate Reform,
              Personhood (Dem. Book)',), ('Corporate Reform, Personhood, Farming
              (Dem. Book)',), ('Crime Rates, Legislation, Education',), ('Debt,
              Credit Cards',), ('Democracy',), ('Population, World',), ('Income',),
              ('Democracy, Corporate
              >
              >
              >
              partial results:
              >
              string a = Agricultural subsidies; Foreign aid
              newstring = Agricultural subsidies; Foreign aid
              just split ['Agricultural subsidies; Foreign aid']
              newstring = Agriculture; Sustainable Agriculture - Support; Organic
              Agriculture; Pesticides, US, Childhood Development, Birth Defects;
              Toxic Chemicals
              just split ['Agriculture; Sustainable Agriculture - Support; Organic
              Agriculture; Pesticides, US, Childhood Development, Birth Defects;
              Toxic Chemicals']
              newstring = Antibiotics, Animals
              just split ['Antibiotics, Animals']

              Comment

              • Fredrik Lundh

                #8
                Re: Why isn't SPLIT splitting my strings

                John Henry wrote:
                Oops!
                for cases like this, writing

                "[" + re.escape(chars et) + "]"

                is usually a good way to avoid repeated oops:ing.
                newString= re.split("[; ()\[\]", result)
                >>newString= re.split("[; ()\[\]", result)
                Traceback (most recent call last):
                ...
                sre_constants.e rror: unexpected end of regular expression

                </F>

                Comment

                • John Henry

                  #9
                  Re: Why isn't SPLIT splitting my strings


                  Fredrik Lundh wrote:
                  John Henry wrote:
                  >
                  Oops!
                  >
                  for cases like this, writing
                  >
                  "[" + re.escape(chars et) + "]"
                  >
                  So, like this?

                  newString= re.split("[" + re.escape("; ()[]") + "]", result)
                  is usually a good way to avoid repeated oops:ing.
                  >
                  newString= re.split("[; ()\[\]", result)
                  >
                  >>newString= re.split("[; ()\[\]", result)
                  Traceback (most recent call last):
                  ...
                  sre_constants.e rror: unexpected end of regular expression
                  >
                  Strange. It works for me. Oh, I know, it's missing a "]".

                  newString= re.split("[; ()\[\]]", result)
                  </F>

                  Comment

                  Working...