splitting a long string into a list

Frederic Rentsch anthra.norell at vtxmail.ch
Wed Nov 29 03:59:21 EST 2006


ronrsr wrote:
> still having a heckuva time with this.
>
> 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(',',';')
>
> 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
>
> kw,typekw= ['Agricultural subsidies; Foreign aid;Agriculture;
> Sustainable Agriculture - Support; Organic Agriculture; Pesticides, US,
> Childhood Development, Birth Defects; Toxic Chemicals;Antibiotics,
> Animals;Agricultural Subsidies
>
>
> what I would like is to break the string into a list of the delimited
> words, but have had no luck doing that - I thought split wuld do that,
> but it doens't.
>
> bests,
>
> -rsr-
>
>
>   

 >>> import SE    # http://cheeseshop.python.org/pypi/SE/2.3

 >>> Split_Marker = SE.SE (' ,=|  ;=| ')    # Translates both ',' and 
';' into an arbitrary split mark ('|')
 >>> for item in Split_Marker (longstring).split ('|'): print item

Agricultural subsidies
 Foreign aidAgriculture
Sustainable Agriculture - Support
 Organic Agriculture

... etc.

To get rid of the the leading space on some lines simply add 
corresponding replacements. SE does any number of substitutions in one 
pass. Defining them is a simple matter of writing them up in one single 
string from which the translator object is made:

 >>> Split_Marker = SE.SE (' ,=|  ;=|  ", =|"  "; =|" ')
 >>> for item in Split_Marker (longstring).split ('|'): print item

Agricultural subsidies
Foreign aidAgriculture
Sustainable Agriculture - Support
Organic Agriculture


Regards

Frederic





More information about the Python-list mailing list