Re: splitting a long string into a list
- From: Peter Otten <__peter__@xxxxxx>
- Date: Tue, 28 Nov 2006 08:51:02 +0100
ronrsr wrote:
still having a heckuva time with this.
You don't seem to get it.
here's where it stand - the split function doesn't seem to work the way
i expect it to.
longkw1,type(longkw): Agricultural subsidies; Foreign
aid;Agriculture; Sustainable Agriculture - Support; Organic
Agriculture; Pesticides, US, Childhood Development, Birth Defects;
<type 'list'> 1
longkw.replace(',',';')
'eat, drink, man, woman'sample = "eat, drink; man, woman"
sample.replace(";", ",")
'eat, drink; man, woman'sample
Aha, Python doesn't replace in place, it creates a new string instead.
Agricultural subsidies; Foreign aid;Agriculture; Sustainable
Agriculture - Support; Organic Agriculture; Pesticides, US, Childhood
Development
kw = longkw.split("; ,") #kw is now a list of len 1
['eat', 'drink+man-woman']sample = "eat+-drink+man-woman"
sample.split("+-")
['eat', '-drink', 'man-woman']sample.split("+")
Aha, Python interprets the complete split() argument as the delimiter, not
each of its characters.
Do you think you can combine these two findings to make your code work? You
will have to replace() first and then split().
Peter
.
- References:
- splitting a long string into a list
- From: ronrsr
- Re: splitting a long string into a list
- From: Robert Kern
- Re: splitting a long string into a list
- From: ronrsr
- splitting a long string into a list
- Prev by Date: Question about import and sys.path
- Next by Date: Re: splitting a long string into a list
- Previous by thread: Re: splitting a long string into a list
- Next by thread: Re: splitting a long string into a list
- Index(es):