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

string to list when the contents is a list

2010-02-17 Thread 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 get: a="['a','b']" b=a[1:-1].spli