Re: string to list

2012-06-14 Thread Chris Rebert
On Thu, Jun 14, 2012 at 12:40 AM, Hemanth H.M wrote: list(literal_eval('"aa","bb 'b'","cc"')) > ['aa', 'bb ', 'cc'] > > Strange? Not really. You didn't properly escape the embedded quotation marks in the string itself! So before anything ever even gets passed to literal_eval(), that part is

Re: string to list

2012-06-14 Thread Anoop Thomas Mathew
@group: Sorry for the mistake. @Hemanth: Thank You for pointing out. I just realized that, we should not copy paste from the console. :) atm ___ Life is short, Live it hard. On 14 June 2012 13:09, Hemanth H.M wrote: > @Annop Nice one, but you seem to have missed a parenthesis. > > >>> list(l

Re: string to list

2012-06-14 Thread Hemanth H.M
>>> list(literal_eval('"aa","bb 'b'","cc"')) ['aa', 'bb ', 'cc'] Strange? On Thu, Jun 14, 2012 at 1:09 PM, Hemanth H.M wrote: > @Annop Nice one, but you seem to have missed a parenthesis. > > >>> list(literal_eval("'aa','bb','cc'") should have been >>> > list(literal_eval("'aa','bb','cc'")) >

Re: string to list

2012-06-14 Thread Hemanth H.M
@Annop Nice one, but you seem to have missed a parenthesis. >>> list(literal_eval("'aa','bb','cc'") should have been >>> list(literal_eval("'aa','bb','cc'")) On Thu, Jun 14, 2012 at 12:58 PM, Anoop Thomas Mathew wrote: > >>> list(literal_eval("'aa','bb','cc'") -- *'I am what I am because

Re: string to list

2012-06-14 Thread Anoop Thomas Mathew
Hi, You can use literal_eval from ast package. >>> from ast import literal_eval >>> list(literal_eval("'aa','bb','cc'") this will return ['aa', 'bb', 'cc'] Thanks, Anoop Thomas Mathew atm ___ Life is short, Live it hard. On 14 June 2012 12:28, Shambhu Rajak wrote: > This will do you job:

Re: string to list

2012-06-14 Thread Peter Otten
bruce g wrote: > What is the best way to parse a CSV string to a list? > > For example, how do I parse: > 'AAA,",,",EEE,FFF,GGG' > to get: > ['AAA','BBB,CCC,','EEE','FFF','GGG’] >>> import csv >>> next(csv.reader(['AAA,",,",EEE,FFF,GGG'])) ['AAA', ',,D

RE: string to list

2012-06-14 Thread Shambhu Rajak
This will do you job: >>> a = 'AAA,",,",EEE,FFF,GGG' >>> b = [] >>> for x in a.split(','): ... if (x.find("\"") > -1): ... x = x.strip("\"") ... b.append(x) If you want reduce the lines of code u can go for this option: b = [x.strip("\"") for x in a.split(',')] So J

Re: string to list

2012-06-13 Thread Ian Kelly
On Wed, Jun 13, 2012 at 10:06 PM, Jose H. Martinez wrote: > string.split(',') will give you an array. > > Example: > > 'AAA,",,",EEE,FFF,GGG '.split(',') > > ['AAA', '"', '', '"', 'EEE', 'FFF', 'GGG'] But it incorrectly splits the quoted part. A proper CSV parser (like th

Re: string to list

2012-06-13 Thread Jose H. Martinez
string.split(',') will give you an array. Example: 'AAA,",,",EEE,FFF,GGG '.split(',') ['AAA', '"', '', '"', 'EEE', 'FFF', 'GGG'] On Wed, Jun 13, 2012 at 10:53 PM, Chris Rebert wrote: > n Wed, Jun 13, 2012 at 7:29 PM, bruce g wrote: > > What is the best way to parse a

Re: string to list

2012-06-13 Thread Chris Rebert
n Wed, Jun 13, 2012 at 7:29 PM, bruce g wrote: > What is the best way to parse a CSV string to a list? Use the `csv` module: http://docs.python.org/library/csv.html http://www.doughellmann.com/PyMOTW/csv/ The `StringIO` module can be used to wrap your string as a file-like object for consumption

Re: string to list when the contents is a list

2010-02-18 Thread Rhodri James
On Thu, 18 Feb 2010 14:52:29 -, nn wrote: Wes James wrote: I have been trying to create a list form a string. The string will be a list (this is the contents will look like a list). i.e. "[]" or "['a','b']" The "[]" is simple since I can just check if value == "[]" then return [] Bu

Re: string to list when the contents is a list

2010-02-18 Thread Aahz
In article , Wes James wrote: > >try: >if value=3D=3D'[]' or value=3D=3D'': > value=3D[] >else: > no_brackets =3D value[1:-1] # s.strip(' \t[]') > c =3D csv.reader([no_brackets], quotechar=3D"'") > value=3Dc.n

Re: string to list when the contents is a list

2010-02-18 Thread Benjamin Kaplan
On Thu, Feb 18, 2010 at 2:56 PM, Wes James wrote: > > I get an error (when I take the "try" out): > > AttributeError: 'function' object has no attribute 'reader' > You have a function called "csv" that's defined after the import csv statement is executed. That function has no attribute 'reader",

Re: string to list when the contents is a list

2010-02-18 Thread Tim Chase
import csv class IS_LIST(): def __init__(self, format='', error_message='must be a list!'): self.format = format self.error_message = error_message def __call__(self, value): try: if value=='[]' or value=='': value=[] else:

Re: string to list when the contents is a list

2010-02-18 Thread Wes James
On Thu, Feb 18, 2010 at 12:32 PM, Wes James wrote: > On Thu, Feb 18, 2010 at 8:18 AM, Tim Chase > wrote: >> Wes James wrote: > > >> >> Just to add to the list of solutions I've seen, letting the built-in csv >> module do the heavy lifting: >> >>  >>> s = "['a','b']" >>  >>> import csv >>  >>> no

Re: string to list when the contents is a list

2010-02-18 Thread Wes James
On Thu, Feb 18, 2010 at 8:18 AM, Tim Chase wrote: > Wes James wrote: > > Just to add to the list of solutions I've seen, letting the built-in csv > module do the heavy lifting: > >  >>> s = "['a','b']" >  >>> import csv >  >>> no_brackets = s[1:-1] # s.strip(' \t[]') >  >>> c = csv.reader([no_br

Re: string to list when the contents is a list

2010-02-18 Thread Tim Chase
Wes James wrote: I have been trying to create a list form a string. The string will be a list (this is the contents will look like a list). i.e. "[]" or "['a','b']" The "[]" is simple since I can just check if value == "[]" then return [] But with "['a','b']" I have tried and get: a="['a','b

Re: string to list when the contents is a list

2010-02-18 Thread nn
Wes James wrote: > I have been trying to create a list form a string. The string will be > a list (this is the contents will look like a list). i.e. "[]" or > "['a','b']" > > The "[]" is simple since I can just check if value == "[]" then return [] > > But with "['a','b']" I have tried and get:

Re: string to list when the contents is a list

2010-02-17 Thread Ben Finney
Wes James writes: > I have been trying to create a list form a string. The string will be > a list (this is the contents will look like a list). i.e. "[]" or > "['a','b']" Pulling back to ask about the larger problem: Are you trying to create Python data structures from a serialised representa

Re: string to list when the contents is a list

2010-02-17 Thread Matt McCredie
Wes James gmail.com> writes: > > I have been trying to create a list form a string. The string will be > a list (this is the contents will look like a list). i.e. "[]" or > "['a','b']" > > The "[]" is simple since I can just check if value == "[]" then return [] > > But with "['a','b']" I ha

Re: string to list when the contents is a list

2010-02-17 Thread Steven D'Aprano
On Thu, 18 Feb 2010 00:13:05 +, Rhodri James wrote: > On Wed, 17 Feb 2010 23:48:38 -, Wes James > wrote: > >> I have been trying to create a list form a string. The string will be >> a list (this is the contents will look like a list). i.e. "[]" or >> "['a','b']" > > If your string is

Re: string to list when the contents is a list

2010-02-17 Thread Rhodri James
On Wed, 17 Feb 2010 23:48:38 -, Wes James wrote: I have been trying to create a list form a string. The string will be a list (this is the contents will look like a list). i.e. "[]" or "['a','b']" If your string is trusted (i.e. goes nowhere near a user), just eval() it. -- Rhodri Jame

Re: string to list when the contents is a list

2010-02-17 Thread Vlastimil Brom
2010/2/18 Wes James : > I have been trying to create a list form a string.  The string will be > a list (this is the contents will look like a list).  i.e. "[]" or > "['a','b']" > > The "[]" is simple since I can just check if value == "[]" then return [] > > But with "['a','b']" I have tried and g

Re: String to List Question

2009-07-02 Thread Rhodri James
On Thu, 02 Jul 2009 23:05:46 +0100, Hanna Michelsen wrote: Hi, I am brand new to python and I love it, but I've been having some trouble with a file parser that I've been working on. It contains lines that start with a name and then continue with names, nicknames and phone numbers of peop

Re: String to List Question

2009-07-02 Thread Philip Semanchuk
On Jul 2, 2009, at 6:05 PM, Hanna Michelsen wrote: Hi, I am brand new to python and I love it, but I've been having some trouble with a file parser that I've been working on. It contains lines that start with a name and then continue with names, nicknames and phone numbers of people asso

Re: string to list conversion

2009-02-19 Thread Srinivas
John, Try the following code .. hope this helps and solves your problem . I have run in the interactive mode >>> s='' >>> a=[s,'12'] >>> print a ['', '12'] regards Srinivas -- http://mail.python.org/mailman/listinfo/python-list

Re: string to list conversion

2009-02-19 Thread MRAB
John Forse wrote: I need to convert an input string say '' to a list of the form ['' ,]. If I use list(stringname), I get ['x','x','x','x'] ; list.join() is an error; and str.join() won't use lists. I do need the comma after the string. Is there a simple solution? Have you tried [st

Re: string to list conversion

2009-02-19 Thread Steve Holden
John Forse wrote: > I need to convert an input string say '' to a list of the form > ['' ,]. If I use list(stringname), I get ['x','x','x','x'] ; > list.join() is an error; and str.join() won't use lists. I do need the > comma after the string. Is there a simple solution? Suppose your inp

Re: String To List

2008-03-17 Thread castironpi
On Mar 17, 10:26 am, Jeff Schwab <[EMAIL PROTECTED]> wrote: > 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

Re: String To List

2008-03-17 Thread Jeff Schwab
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

Re: String To List

2008-03-17 Thread castironpi
> > > > 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.

Re: String To List

2008-03-17 Thread Tom Stambaugh
It's too bad your inner data items are delimited with an apostrophe (') instead a double-quote ("). If they were double-quote, you could do something as simple as: Given: a = '["xyz", "abc"]' import simplejson answer = simplejson.loads(a) There may be an incantation to simplejson that allows y

Re: String To List

2008-03-17 Thread Iain King
On Mar 17, 9:27 am, Iain King <[EMAIL PROTECTED]> wrote: > On Mar 17, 6:56 am, Dan Bishop <[EMAIL PROTECTED]> wrote: > > > On Mar 17, 1:15 am, Girish <[EMAIL PROTECTED]> wrote: > > > > I have a string a = "['xyz', 'abc']".. I would like to convert it to a > > > list with elements 'xyz' and 'abc'. I

Re: String To List

2008-03-17 Thread George Sakkis
On Mar 17, 3:22 am, Paul Rubin wrote: > Girish <[EMAIL PROTECTED]> writes: > > 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 care

Re: String To List

2008-03-17 Thread Iain King
On Mar 17, 6:56 am, Dan Bishop <[EMAIL PROTECTED]> wrote: > On Mar 17, 1:15 am, Girish <[EMAIL PROTECTED]> 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?? > > Thanks for the help.

Re: String To List

2008-03-17 Thread Paul Rubin
Girish <[EMAIL PROTECTED]> writes: > 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. M

Re: String To List

2008-03-17 Thread Dan Bishop
On Mar 17, 1:15 am, Girish <[EMAIL PROTECTED]> 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?? > Thanks for the help... eval(a) will do the job, but you have to be very careful about usi

Re: string to list of numbers conversion

2006-11-10 Thread Tim Williams
On 5 Nov 2006 04:34:32 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hi, > I have a string '((1,2), (3,4))' and I want to convert this into a > python tuple of numbers. But I do not want to use eval() because I do > not want to execute any code in that string and limit it to list of > num

Re: string to list of numbers conversion

2006-11-08 Thread henning
[EMAIL PROTECTED] skrev: > Hi, > I have a string '((1,2), (3,4))' and I want to convert this into a > python tuple of numbers. I think your question is deeper and more natural than is clear from the many recepies given so far in this thread, so I'll take on another point of view, >From a langu

Re: string to list of numbers conversion

2006-11-06 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: > I have a string '((1,2), (3,4))' and I want to convert this into a > python tuple of numbers. But I do not want to use eval() because I do > not want to execute any code in that string and limit it to list of > numbers. here's yet another approach: http://online.effb

Re: string to list of numbers conversion

2006-11-06 Thread Paul McGuire
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hi, > I have a string '((1,2), (3,4))' and I want to convert this into a > python tuple of numbers. But I do not want to use eval() because I do > not want to execute any code in that string and limit it to list of

Re: string to list of numbers conversion

2006-11-06 Thread Frederic Rentsch
[EMAIL PROTECTED] wrote: > Hi, > I have a string '((1,2), (3,4))' and I want to convert this into a > python tuple of numbers. But I do not want to use eval() because I do > not want to execute any code in that string and limit it to list of > numbers. > Is there any alternative way? > > Thanks

Re: string to list of numbers conversion

2006-11-05 Thread Peter Otten
[EMAIL PROTECTED] wrote: > This recipe fails when negative numbers are used. > > safe_eval('(12, -12)') > *** Unsafe_Source_Error: Line 1. Unsupported source construct: > compiler.ast.UnarySub > > But, I think it could be easily fixed for somebody who understands the > script. I think that s

Re: string to list of numbers conversion

2006-11-05 Thread bearophileHUGS
[EMAIL PROTECTED] wrote: > Hi, > I have a string '((1,2), (3,4))' and I want to convert this into a > python tuple of numbers. But I do not want to use eval() because I do > not want to execute any code in that string and limit it to list of > numbers. > Is there any alternative way? This is

Re: string to list of numbers conversion

2006-11-05 Thread [EMAIL PROTECTED]
Peter, Thanks. This recipe fails when negative numbers are used. safe_eval('(12, -12)') *** Unsafe_Source_Error: Line 1. Unsupported source construct: compiler.ast.UnarySub But, I think it could be easily fixed for somebody who understands the script. Can somebody help. Thanks. Suresh Peter O

Re: string to list of numbers conversion

2006-11-05 Thread Peter Otten
[EMAIL PROTECTED] wrote: > I have a string '((1,2), (3,4))' and I want to convert this into a > python tuple of numbers. But I do not want to use eval() because I do > not want to execute any code in that string and limit it to list of > numbers. > Is there any alternative way? http://aspn.ac

Re: string to list of numbers conversion

2006-11-05 Thread Gerard Flanagan
[EMAIL PROTECTED] wrote: > Hi, > I have a string '((1,2), (3,4))' and I want to convert this into a > python tuple of numbers. But I do not want to use eval() because I do > not want to execute any code in that string and limit it to list of > numbers. > Is there any alternative way? Python