By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,847 Members | 2,402 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,847 IT Pros & Developers. It's quick & easy.

Why isn't SPLIT splitting my strings

P: n/a
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']

Nov 19 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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']
Nov 19 '06 #2

P: n/a
"ronrsr" <ro****@gmail.comwrote:
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.split)
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.
>>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.
Nov 19 '06 #3

P: n/a
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.split)
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>

Nov 19 '06 #4

P: n/a
"ronrsr" <ro****@gmail.comwrote in message
news:11**********************@h54g2000cwb.googlegr oups.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
Nov 19 '06 #5

P: n/a
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):
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']
Nov 19 '06 #6

P: n/a
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):
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']
Nov 19 '06 #7

P: n/a
John Henry wrote:
Oops!
for cases like this, writing

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

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

</F>

Nov 19 '06 #8

P: n/a

Fredrik Lundh wrote:
John Henry wrote:
Oops!

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)
</F>
Nov 19 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.