473,388 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

String To List

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
8 2421
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
5
by: oliver | last post by:
hi there i'm experimanting with imaplib and came across stringts like (\HasNoChildren) "." "INBOX.Sent Items" in which the quotes are part of the string. now i try to convert this into a...
2
by: s | last post by:
I'm getting compile errors on the following code: <code> #include <iostream> #include <fstream> #include <list> #include <string> using namespace std;
4
by: blrmaani | last post by:
Here is what I want: string s1 = "This is a list of string"; list<string> s2 = s1.some_method(); Now, I should be able to traverse list s2 and get each member ( which is of type 'string' ). ...
5
by: SpotNet | last post by:
Hello NewsGroup, I have a custom class and a collection for that custom class that inherits CollectionBase. As such; public class MyClass { private string datamember1 = string.Empty,...
6
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: ...
9
by: incredible | last post by:
how to sort link list of string
1
by: kellysgirl | last post by:
Now what you are going to see posted here is both the set of instructions I was given..and the code I have written. The instructions I was given are as follows In this case, you will create...
12
by: aparnakakkar2003 | last post by:
can any one tell me if I give the followiing string in input: ABC abc BBC then how I can get ABC abc BBC
4
by: parez | last post by:
Hi, I am trying to serialize List<List<string>. With the following code public List<List<string>DataRows { get; set; }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.