Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5351

Re: How best to convert a string "list" to a python list

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!news-transit.tcx.org.uk!nntp-feed.chiark.greenend.org.uk!ewrotcd!feeds.news.ox.ac.uk!news.ox.ac.uk!zen.net.uk!hamilton.zen.co.uk!shaftesbury.zen.co.uk.POSTED!not-for-mail
From Nobody <nobody@nowhere.com>
Subject Re: How best to convert a string "list" to a python list
Date Sat, 14 May 2011 09:41:20 +0100
User-Agent Pan/0.14.2 (This is not a psychotic episode. It's a cleansing moment of clarity.)
Message-Id <pan.2011.05.14.08.40.47.47000@nowhere.com>
Newsgroups comp.lang.python
References <2efc3a05-22e5-46db-bd22-c95221bbd7b2@k16g2000yqm.googlegroups.com>
MIME-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
Lines 19
Organization Zen Internet
NNTP-Posting-Host a6194155.news.zen.co.uk
X-Trace DXC=2RC<9X5X>>3Hf6nZcdIbW>nok4Z\<mH49hDU:2=XKPk8VIR>3cB^f=:QOMCU8L]Sh72KaT5_L<KN0
X-Complaints-To abuse@zen.co.uk
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5351

Show key headers only | View raw


On Fri, 13 May 2011 10:15:29 -0700, noydb 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?

> x = "red;blue;green;yellow" ## string of semi-colon delimited colors

Provided that a semicolon is *always* a delimiter, just use the .split()
method:

	color_list = x.split(";")

For more complex formats, where there are quote and/or escape characters
which allow the delimiter to occur as part of an item, you typically need
to use a regular expression to match everything up to the next delimiter,
and do this in a loop to extract the individual items.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How best to convert a string "list" to a python list noydb <jenn.duerr@gmail.com> - 2011-05-13 10:15 -0700
  Re: How best to convert a string "list" to a python list Jerry Hill <malaclypse2@gmail.com> - 2011-05-13 13:30 -0400
  Re: How best to convert a string "list" to a python list MRAB <python@mrabarnett.plus.com> - 2011-05-13 18:36 +0100
  Re: How best to convert a string "list" to a python list Redcat <redcat@catfolks.net> - 2011-05-13 19:26 +0000
  Re: How best to convert a string "list" to a python list Nobody <nobody@nowhere.com> - 2011-05-14 09:41 +0100
    Re: How best to convert a string "list" to a python list Daniel Kluev <dan.kluev@gmail.com> - 2011-05-14 21:16 +1100
    Re: How best to convert a string "list" to a python list Terry Reedy <tjreedy@udel.edu> - 2011-05-14 15:59 -0400

csiph-web