By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,713 Members | 764 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,713 IT Pros & Developers. It's quick & easy.

String To List

P: n/a
I have a string a = "['xyz', 'abc']".. I would like to convert it to a
list with elements 'xyz' and 'abc'. Is there any simple solution for
this??
Thanks for the help...
Mar 17 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Mar 17, 1:15 am, Girish <girish....@gmail.comwrote:
I have a string a = "['xyz', 'abc']".. I would like to convert it to a
list with elements 'xyz' and 'abc'. Is there any simple solution for
this??
Thanks for the help...
eval(a) will do the job, but you have to be very careful about using
that function. An alternative is

[s.strip('\'"') for s in a.strip('[]').split(', ')]
Mar 17 '08 #2

P: n/a
Girish <gi********@gmail.comwrites:
I have a string a = "['xyz', 'abc']".. I would like to convert it to a
list with elements 'xyz' and 'abc'. Is there any simple solution for
this??
Thanks for the help...
Be careful about using eval, if the string came from a potentially
hostile source. Maybe what you really want is JSON, which has
python-like syntax but a bunch of safe parsers.
Mar 17 '08 #3

P: n/a
On Mar 17, 6:56 am, Dan Bishop <danb...@yahoo.comwrote:
On Mar 17, 1:15 am, Girish <girish....@gmail.comwrote:
I have a string a = "['xyz', 'abc']".. I would like to convert it to a
list with elements 'xyz' and 'abc'. Is there any simple solution for
this??
Thanks for the help...

eval(a) will do the job, but you have to be very careful about using
that function. An alternative is

[s.strip('\'"') for s in a.strip('[]').split(', ')]
This will fall over if xyz or abc include any of the characters your
stripping/splitting on (e.g if xyz is actually "To be or not to be,
that is the question"). Unless you can guarantee they won't, you'll
need to write (or rather use) a parser that understands the syntax.

Iain
Mar 17 '08 #4

P: n/a
On Mar 17, 3:22 am, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
Girish <girish....@gmail.comwrites:
I have a string a = "['xyz', 'abc']".. I would like to convert it to a
list with elements 'xyz' and 'abc'. Is there any simple solution for
this??
Thanks for the help...

Be careful about using eval, if the string came from a potentially
hostile source. Maybe what you really want is JSON, which has
python-like syntax but a bunch of safe parsers.
Or take a look at a restricted safe eval variant (e.g.
http://groups.google.com/group/comp....2d479569b1712e)

George
Mar 17 '08 #5

P: n/a
On Mar 17, 9:27 am, Iain King <iaink...@gmail.comwrote:
On Mar 17, 6:56 am, Dan Bishop <danb...@yahoo.comwrote:
On Mar 17, 1:15 am, Girish <girish....@gmail.comwrote:
I have a string a = "['xyz', 'abc']".. I would like to convert it to a
list with elements 'xyz' and 'abc'. Is there any simple solution for
this??
Thanks for the help...
eval(a) will do the job, but you have to be very careful about using
that function. An alternative is
[s.strip('\'"') for s in a.strip('[]').split(', ')]

This will fall over if xyz or abc include any of the characters your
stripping/splitting on (e.g if xyz is actually "To be or not to be,
that is the question"). Unless you can guarantee they won't, you'll
need to write (or rather use) a parser that understands the syntax.

Iain

Thinking about this some more; could the string module not use a
simple tokenizer method? I know that relentlessly adding features to
built-ins is a bad idea, so I'm not sure if this falls within
batteries-included, or is actually just adding bulk. On the one hand,
it's not difficult to write a simple state-based token parser
yourself, but on the other it is also quite easy to include a pile of
bugs when you do. By simple I mean something like:

def tokenize(string, delim, closing_delim=None, escape_char=None)

which would return a list (or a generator) of all the parts of the
string enclosed by delim (or which begin with delim and end with
closing_delim if closing_delim is set), ignoring any delimiters which
have been escaped by escape_char. Throw an exception if the string
is malformed? (odd number of delimiters, or opening/closing delims
don't match)

In the OP's case, he could get what he want's with a simple: l =
a.tokenize("'")

The point of this ramble not being that this is a how to solve the
OP's question, but wondering if it would be a good inclusion to the
language in general. Or there's actually a module which already does
it that I couldn't find and I'm an idiot...

Iain
Mar 17 '08 #6

P: n/a
I have a string a = "['xyz', 'abc']".. I would like to convert it to a
list with elements 'xyz' and 'abc'. Is there any simple solution for
this??
Thanks for the help...
eval(a) will do the job, but you have to be very careful about using
that function. *An alternative is
[s.strip('\'"') for s in a.strip('[]').split(', ')]
This will fall over if xyz or abc include any of the characters your
stripping/splitting on (e.g if xyz is actually "To be or not to be,
that is the question"). *Unless you can guarantee they won't, you'll
need to write (or rather use) a parser that understands the syntax.
Iain

Thinking about this some more; could the string module not use a
simple tokenizer method? *I know that relentlessly adding features to
built-ins is a bad idea, so I'm not sure if this falls within
batteries-included, or is actually just adding bulk. *On the one hand,
it's not difficult to write a simple state-based token parser
yourself, but on the other it is also quite easy to include a pile of
bugs when you do. *By simple I mean something like:

def tokenize(string, delim, closing_delim=None, escape_char=None)

which would return a list (or a generator) of all the parts of the
string enclosed by delim (or which begin with delim and end with
closing_delim if closing_delim is set), ignoring any delimiters which
have been escaped by escape_char. * Throw an exception if the string
is malformed? (odd number of delimiters, or opening/closing delims
don't match)

In the OP's case, he could get what he want's with a simple: * l =
a.tokenize("'")
Slippery slope, though, to nested delimiters, and XML after that.
Where does shlex get us? Do we want to parse "['xyz', 'abc',
['def','ghi']]" any special way? Are there security concerns past a
really low complexity level, such as recursion overflows?
Mar 17 '08 #7

P: n/a
Girish wrote:
I have a string a = "['xyz', 'abc']".. I would like to convert it to a
list with elements 'xyz' and 'abc'. Is there any simple solution for
this??
Do you want:

(1) Specifically to vivify lists formatted as in your example? If so, why?

(2) To save and restore arbitrary python objects?

(3) To define some kind of configuration file format that you can read
from Python?
Mar 17 '08 #8

P: n/a
On Mar 17, 10:26*am, Jeff Schwab <j...@schwabcenter.comwrote:
Girish wrote:
I have a string a = "['xyz', 'abc']".. I would like to convert it to a
list with elements 'xyz' and 'abc'. Is there any simple solution for
this??

Do you want:

(1) Specifically to vivify lists formatted as in your example? *If so, why?

(2) To save and restore arbitrary python objects?

(3) To define some kind of configuration file format that you can read
from Python?
Bar says: Announce your intentions, then contents. (Form, then
contents.) == List of two strings.

How does that go into code?
>>list([str,str])
[<type 'str'>, <type 'str'>]
Mar 18 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.