Why isn't SPLIT splitting my strings

Paul McGuire ptmcg at austin.rr._bogus_.com
Sun Nov 19 05:42:45 EST 2006


"ronrsr" <ronrsr at gmail.com> wrote in message 
news:1163931285.390782.130500 at h54g2000cwb.googlegroups.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(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
>
>
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,Cotton
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,Personhood (Dem. Book)
Corporate Reform,  Personhood, Farming(Dem. Book)
Crime Rates, Legislation, Education
Debt,Credit Cards
Democracy
Population, World
Income 





More information about the Python-list mailing list