472,371 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

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(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
8 2296
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
"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
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
"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
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Will McGugan | last post by:
Hi, I'm curious about the behaviour of the str.split() when applied to empty strings. "".split() returns an empty list, however.. "".split("*") returns a list containing one empty string. ...
5
by: Vamsi | last post by:
Hi, I am trying a basic opearation of splitting a multiline value to an array of single lines(Actually making Address into AddressLine1, AddressLine2). I used Environment.NewLine in split, I...
6
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
19
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple...
5
by: kurt sune | last post by:
The code: Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" & vbNewLine Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf) Dim csvColumns2 As String() =...
4
by: Michele Petrazzo | last post by:
Hello ng, I don't understand why split (string split) doesn't work with the same method if I can't pass values or if I pass a whitespace value: >>> "".split() >>> "".split(" ") But into...
4
by: thiago_bagua | last post by:
Hi all, I have a csv file exported from excel. On cells where there was a comma in the text, it wraps the character with double quotes. For example: Column 1,"column two, and some more...
7
by: John A Grandy | last post by:
Is string.Split() performant to a degree that there's no point in trying to write code to improve on its performance ?
14
by: Stevo | last post by:
If you split a string into an array using the split method, it's not working the way I'd expect it to. That doesn't mean it's wrong of course, but would anyone else agree it's working somewhat...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.