Re: splitting a long string into a list



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

Here you have discovered that string.replace() returns a string and does
NOT modify the original string. Try this for clarification:

a="DAWWIJFWA,dwadw;djwkajdw"
a
'DAWWIJFWA,,,,,,dwadw;djwkajdw'
a.replace(",",";")
'DAWWIJFWA;;;;;;dwadw;djwkajdw'
a
'DAWWIJFWA,,,,,,dwadw;djwkajdw'
b = a.replace(',',';')
b
'DAWWIJFWA;;;;;;dwadw;djwkajdw'





kw = longkw.split("; ,") #kw is now a list of len 1

Yes, because it is trying to split longkw wherever it finds the whole
string "; '" and NOT wherever it finds ";" or " " or ",". This has been
stated before by NickV, Duncan Booth, Fredrik Lundh and Paul McGuire
amongst others. You will need to do either:

a.)

# First split on every semicolon
a = longkw.split(";")
b = []
# Then split those results on whitespace
#(the default action for string.split())
for item in a:
b.append(item.split())
# Then split on commas
kw = []
for item in b:
kw.append(item.split(","))

or b.)

# First replace commas with spaces
longkw = longkw.replace(",", " ")
# Then replace semicolons with spaces
longkw = longkw.replace(";", " ")
# Then split on white space, (default args)
kw = longkw.split()


Note that we did:
longkw = longkw.replace(",", " ")
and not just:
longkw.replace(",", " ")


You will find that method A may give empty strings as some elements of
kw. If so, use method b.


Finally, if you have further problems, please please do the following:

1.) Provide your input data clearly, exactly as you have it.
2.) Show exactly what you want the output to be, including any special
cases.
3.) If something doesn't work the way you expect it to, tell us how you
expect it to work so we know what you mean by "doesn't work how I expect
it to"
4.) Read all the replies carefully and if you don't understand the
reply, ask for clarification.
5.) Read the help functions carefully - what the input parameters have
to be and what the return value will be, and whether or not it changes
the parameters or original object. Strings are usually NOT mutable so
any functions that operate on strings tend to return the result as a new
string and leave the original string intact.

I really hope this helps,

Cameron.
.



Relevant Pages

  • Re: determining the bounds of a tuple returned from a database
    ... i think I'm misunderstanding what lendoes - to me they appear to ... or at least be composed of a string of some ... can iterate through them. ... 3: Agricultural Subsidies, Global Trade ...
    (comp.lang.python)
  • Re: Splitting a large string variable into lines <= 70 chars
    ... I kinda agree with you that perhaps the string search methods and functions like InStrRev and LastIndexOf will not be the fastest way. ... Wait till they grumble it's slow and then throw in a faster routine and your a hero again! ... All he'd done was reduce the number of iterations his code spent in the wait loops. ... Instead, do not modify the original string at all during the loop, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How do you remove all the symbols?
    ... The fact the msgbox shows "hidden message oxford st" shows the function is ... new changed string. ... If you are only using macros and want to change your original string you can ... Dim sOld As String ...
    (microsoft.public.excel.programming)
  • Re: Comment on trim string function please
    ... If the char type on any machine ... How do you think the characters wound up in a string in the first ... original string: ...
    (comp.lang.c)
  • Re: strtok ( ) help
    ... >>> string between calls to strtok() its copy would no longer match. ... >> operation changed the original string between calls to strtokits ... > has to have a pointer into that string in all cases. ...
    (comp.lang.c)