On Fri, May 13, 2011 at 11:15 AM, noydb <[email protected]> wrote:
> I want some code to take the items in a semi-colon-delimted string
> "list" and places each in a python list. I came up with below. In
> the name of learning how to do things properly, do you experts have a
> better way of doing it?
>
> Thanks for any inputs!
>
> ***
> x = "red;blue;green;yellow" ## string of semi-colon delimited colors
>
> color_list = []
> ## sc = semi-colon
>
> while x.find(";") <> -1:
> sc_pos = x.find(";")
> current_color = x[0:sc_pos] ## color w/o sc
> current_color_sc = x[0:sc_pos+1] ## color with sc
> color_list.append(current_color) ## append color to list
> x = x.replace(current_color_sc, "") ## remove color and sc from
> string
> print current_color
> print color_list
> print x + "\n"
>
> color_list.append(x) # append last color left in x (no sc at end of
> string)
> print color_list
>
> print "done"
> --
> http://mail.python.org/mailman/listinfo/python-list
>
Try the following:
color_list = x.split(";")
Python string objects have a variety of helpful methods. Take a look at
http://docs.python.org/library/stdtypes.html#string-methods. Hope that
helps.
-eric
--
http://mail.python.org/mailman/listinfo/python-list