Why isn't SPLIT splitting my strings

Nick Vatamaniuc vatamane at gmail.com
Sun Nov 19 05:31:26 EST 2006


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):
http://docs.python.org/lib/node46.html

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(result[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']




More information about the Python-list mailing list