Why isn't SPLIT splitting my strings

R

ronrsr

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[0])
print "just split", str(result[0]).split('; ()[]"')
zd.append(str(result[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']
 
N

Nick Vatamaniuc

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.
 
F

Fredrik Lundh

ronrsr said:
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.

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>
 
D

Duncan Booth

ronrsr said:
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 on method_descriptor:

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.translate or
str.replace to replace all of the separators with the same character and
split on that. e.g.
s = s.replace(' ', ';')
s = s.replace('(', ';')
s = s.replace(')', ';')
s = s.replace('[', ';')
s = s.replace(']', ';')
s = s.replace('"', ';')
return s.split(';')
['Antibiotics,', 'Animals']

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

Paul McGuire

ronrsr said:
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[0])
print "just split", str(result[0]).split('; ()[]"')
zd.append(str(result[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
 
J

John Henry

Meaning:

import re

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



Nick said:
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.

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[0])
print "just split", str(result[0]).split('; ()[]"')
zd.append(str(result[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']
 
J

John Henry

Oops!

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

John said:
Meaning:

import re

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



Nick said:
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.

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[0])
print "just split", str(result[0]).split('; ()[]"')
zd.append(str(result[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']
 
J

John Henry

Fredrik said:
John said:

for cases like this, writing

"[" + re.escape(charset) + "]"

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.error: unexpected end of regular expression

Strange. It works for me. Oh, I know, it's missing a "]".

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top