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
t; -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 Just Cheerz, > -Shambhu > > -Original Messa

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
he lines of code u can go for this option: b = [x.strip("\"") for x in a.split(',')] So Just Cheerz, -Shambhu -Original Message- From: bruce g [mailto:bruceg113...@gmail.com] Sent: 14/06/2012 8:00 AM To: python-list@python.org Subject: string to list What is th

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: Converting a string to list for submission to easygui multenterb​ox

2012-05-02 Thread Chris Angelico
On Thu, May 3, 2012 at 3:57 AM, Laurent Pointal wrote: > If you have it as a string, you can use eval() (not safe!) on the string to > retrieve the tuple, then list() on the tuple to get a list. Are you saying that eval is not safe (which it isn't), or that it has to be eval() and not safe_eval()

Re: Converting a string to list for submission to easygui multenterb​ox

2012-05-02 Thread ksals
On May 2, 1:57 pm, Laurent Pointal wrote: > ksals wrote: > > On May 1, 5:29 pm, John Gordon wrote: > >> In <3b5f65c4-cd95-4bb4-94f2-0c69cf2b1...@d20g2000vbh.googlegroups.com> > >> ksals writes: > > >> > The original choice looks like this when I print it: > >> > print(choice) > >> > ('ksals', ''

Re: Converting a string to list for submission to easygui multenterb​ox

2012-05-02 Thread Laurent Pointal
ksals wrote: > On May 1, 5:29 pm, John Gordon wrote: >> In <3b5f65c4-cd95-4bb4-94f2-0c69cf2b1...@d20g2000vbh.googlegroups.com> >> ksals writes: >> >> > The original choice looks like this when I print it: >> > print(choice) >> > ('ksals', '', 'alsdkfj', '3', '') >> > I need to submit these as de

Re: Converting a string to list for submission to easygui multenterb​ox

2012-05-01 Thread Cameron Simpson
The tutorial suggests multchoicebox returns an interable of chosen items, in fact probably a seqeunce. So... On 01May2012 14:50, ksals wrote: | This is a small excert to show you what I get | | for choice in easygui.multchoicebox(msg1, title,qstack): | if choice[0] == None: |

Re: Converting a string to list for submission to easygui multenterb​ox

2012-05-01 Thread Cameron Simpson
Disclaimer: I have never used esygui. On 01May2012 21:29, John Gordon wrote: | In <3b5f65c4-cd95-4bb4-94f2-0c69cf2b1...@d20g2000vbh.googlegroups.com> ksals writes: | > The original choice looks like this when I print it: | | > print(choice) | > ('ksals', '', 'alsdkfj', '3', '') That's just pr

Re: Converting a string to list for submission to easygui multenterb​ox

2012-05-01 Thread ksals
On May 1, 5:29 pm, John Gordon wrote: > In <3b5f65c4-cd95-4bb4-94f2-0c69cf2b1...@d20g2000vbh.googlegroups.com> ksals > writes: > > > The original choice looks like this when I print it: > > print(choice) > > ('ksals', '', 'alsdkfj', '3', '') > > I need to submit these as defaults to a multenterb

RE: Converting a string to list for submission to easygui multenterb​ox

2012-05-01 Thread Prasad, Ramit
> > That looks like a tuple which contains five strings. > The original choice looks like this when I print it: > > print(choice) > ('ksals', '', 'alsdkfj', '3', '') Based on the print statement, choice is a tuple or a string. try doing `print(type(choice))`. On the assumption it is a tuple a

Re: Re: Converting a string to list for submission to easygui multenterb​ox

2012-05-01 Thread John Gordon
In <3b5f65c4-cd95-4bb4-94f2-0c69cf2b1...@d20g2000vbh.googlegroups.com> ksals writes: > The original choice looks like this when I print it: > print(choice) > ('ksals', '', 'alsdkfj', '3', '') > I need to submit these as defaults to a multenterbox. Each entry above > ksals, "", "alsdkfj', 3 , '

Re: Converting a string to list for submission to easygui multenterb​ox

2012-05-01 Thread ksals
On May 1, 4:26 pm, John Gordon wrote: > In <316efebe-7f54-4054-96b1-51c7bb7b7...@f5g2000vbt.googlegroups.com> ksals > writes: > > > Please help a newbe.  I have a string returned from an esygui > > multchoicebox that looks like > >   this:  ('ksals', '', 'alsdkfj', '3', '') I need to convert thi

Re: Converting a string to list for submission to easygui multenterb​ox

2012-05-01 Thread Pedro Kroger
Have you tried to use the function list?: foo = (1,2,3) list(foo) Cheers, Pedro -- http://pedrokroger.net On May 1, 2012, at 5:18 PM, ksals wrote: > Please help a newbe. I have a string returned from an esygui > multchoicebox that looks like > this: ('ksals', '', 'alsdkfj', '3', '') I ne

Re: Converting a string to list for submission to easygui multenterb​ox

2012-05-01 Thread John Gordon
In <316efebe-7f54-4054-96b1-51c7bb7b7...@f5g2000vbt.googlegroups.com> ksals writes: > Please help a newbe. I have a string returned from an esygui > multchoicebox that looks like > this: ('ksals', '', 'alsdkfj', '3', '') I need to convert this to > this: ['ksals', '', 'alsdkfj', '3', '']

Converting a string to list for submission to easygui multenterb​ox

2012-05-01 Thread ksals
Please help a newbe. I have a string returned from an esygui multchoicebox that looks like this: ('ksals', '', 'alsdkfj', '3', '') I need to convert this to this: ['ksals', '', 'alsdkfj', '3', ''] This is so I can submit this to a multenterbox with 5 fields -- http://mail.python.org/mailm

Converting a string to list for submission to easygui multenterbox

2012-05-01 Thread ksals
Please help a newbe. I have a string returned from an esygui multchoicebox that looks like this: ('ksals', '', 'alsdkfj', '3', '') I need to convert this to this: ['ksals', '', 'alsdkfj', '3', ''] This is so I can submit this to a multchoicebox with 5 fields -- http://mail.python.org/mailm

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

Re: Quick compare string to list

2009-09-30 Thread Steven D'Aprano
On Wed, 30 Sep 2009 11:36:03 -0700, Scooter wrote: > I'm reading in a text file, and for each line in the file, I'm looking > for the existence of phrases from a list. The list contains approx. 120 > items currently but will most likely grow. This procedure itself is not > the main function of my

Re: Quick compare string to list

2009-09-30 Thread Bearophile
Scooter: > I'm reading in a text file, and for each line in the file, I'm looking > for the existence of phrases from a list. The list contains approx. > 120 items currently but will most likely grow. This procedure itself > is not the main function of my program and only grew out of the need > to

Re: Quick compare string to list

2009-09-30 Thread Emile van Sebille
On 9/30/2009 11:36 AM Scooter said... I'm reading in a text file, and for each line in the file, I'm looking for the existence of phrases from a list. The list contains approx. 120 items currently but will most likely grow. This procedure itself is not the main function of my program and only gre

Re: Quick compare string to list

2009-09-30 Thread Terry Reedy
Scooter wrote: I'm reading in a text file, and for each line in the file, I'm looking for the existence of phrases from a list. The list contains approx. 120 items currently but will most likely grow. This procedure itself is not the main function of my program and only grew out of the need to re

Quick compare string to list

2009-09-30 Thread Scooter
I'm reading in a text file, and for each line in the file, I'm looking for the existence of phrases from a list. The list contains approx. 120 items currently but will most likely grow. This procedure itself is not the main function of my program and only grew out of the need to reformat certain ph

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

String to List Question

2009-07-02 Thread Hanna Michelsen
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 associated with that name. I need to create a list of the names o

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

string to list conversion

2009-02-19 Thread John Forse
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? Regards John -- http://mail.pytho

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

String To List

2008-03-16 Thread Girish
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... -- http://mail.python.org/mailman/listinfo/python-list

Re: Convert String to list of chars

2007-01-27 Thread Vlad Dogaru
On Jan 27, 9:18 am, Neil Cerutti <[EMAIL PROTECTED]> wrote: > On 2007-01-27, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > > How can I convert a string to a char list? > > for example > > > "hello" --> ['h','e','l','l','o'] > > > I have been searching but I can't find my answers > list("hello")

Re: Convert String to list of chars

2007-01-26 Thread Neil Cerutti
On 2007-01-27, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > How can I convert a string to a char list? > for example > > "hello" --> ['h','e','l','l','o'] > > I have been searching but I can't find my answers list("hello") -- http://mail.python.org/mailman/listinfo/python-list

Convert String to list of chars

2007-01-26 Thread juanefren
How can I convert a string to a char list? for example "hello" --> ['h','e','l','l','o'] I have been searching but I can't find my answers thanks -- Juan Efrén Castillo Encinas -- http://mail.python.org/mailman/listinfo/python-list

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

string to list of numbers conversion

2006-11-05 Thread [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 numbers. Is there any alternative way? Thanks. Suresh -- http://mail.python.org/mai

Re: how to convert string to list or tuple

2005-06-02 Thread Duncan Booth
Ruud de Jong wrote: > Steven Bethard schreef: >> But unless the person eval-ing your code *only* writes immaculate >> code I can see that you can probably screw them. ;) I wonder why >> __subclasses__ isn't a restricted attribute... Is it ever used for >> something that isn't evil? ;) >> >> S

Re: how to convert string to list or tuple

2005-06-01 Thread Ruud de Jong
Steven Bethard schreef: > But unless the person eval-ing your code *only* writes immaculate code I > can see that you can probably screw them. ;) I wonder why > __subclasses__ isn't a restricted attribute... Is it ever used for > something that isn't evil? ;) > > STeVe Completely off topic,

Re: how to convert string to list or tuple

2005-06-01 Thread Steven Bethard
Duncan Booth wrote: > Steven Bethard wrote: > > >>Interestingly, I don't seem to be able to create a file object as a >>class attribute in restricted mode: >> >>py> class C(object): >>... def __init__(self): >>... self.f = file('temp.txt', 'w') >>... >>py> eval('''[ cls for cls in >>

Re: how to convert string to list or tuple

2005-06-01 Thread Fuzzyman
flyaflya wrote: > a = "(1,2,3)" > I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', > '2', ',', '3', ')') not (1,2,3) Probably a bit late... but there's always listquote - It's part of the pythonutils module. http://www.voidspace.org.uk/python/pythonutils.html It will turn st

Re: how to convert string to list or tuple

2005-06-01 Thread Duncan Booth
Steven Bethard wrote: > Interestingly, I don't seem to be able to create a file object as a > class attribute in restricted mode: > > py> class C(object): > ... def __init__(self): > ... self.f = file('temp.txt', 'w') > ... > py> eval('''[ cls for cls in > {}.__class__.__bases__[0]._

Re: how to convert string to list or tuple

2005-05-31 Thread Steven Bethard
Duncan Booth wrote: > e.g. Assuming that the MyDatabase class does something nasty to a file: > class MyDatabase(object): > > def __init__(self, filename): > self.filename = filename > def initialise(self): > print "Splat %s" % self.filename > eval('''[ cls for cl

Re: how to convert string to list or tuple

2005-05-31 Thread Duncan Booth
Steven Bethard wrote: > Duncan Booth wrote: >> any new style class you have defined and call any of its methods with >> whatever arguments I wish. > > Any new style class that I've defined? Or just any one I pass in as > part of dict(__builtins__=None, ...)? If the former, could you > elabora

Re: how to convert string to list or tuple

2005-05-30 Thread Steven Bethard
Duncan Booth wrote: > Steven Bethard wrote: > >>But you can try it at home if you set __builtins__ to something other >>than the default: >> >>py> eval("""__import__("os").system('echo "hello"')""", >>dict(__builtins__=None)) >>Traceback (most recent call last): >> File "", line 1, in ? >> F

Re: how to convert string to list or tuple

2005-05-30 Thread Duncan Booth
Steven Bethard wrote: >> Have you tried giving it the string '__import__("os").system("rm -rf >> *")'? [Don't try that at home children!] > > But you can try it at home if you set __builtins__ to something other > than the default: > > py> eval("""__import__("os").system('echo "hello"')""", >

Re: how to convert string to list or tuple

2005-05-29 Thread Steven Bethard
Duncan Booth wrote: > Dan Bishop wrote: >> Or if you do use eval, don't give it access to any names. [snip] >> os.system("rm -rf *") >> Traceback (most recent call last): >> File "", line 1, in ? >> File "", line 0, in ? >> NameError: name 'os' is not defined > > Have you tried giving it the s

Re: how to convert string to list or tuple

2005-05-29 Thread John Roth
"Duncan Booth" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Dan Bishop wrote: > >> Simon Brunning wrote: >>> [...] >> >> Or if you do use eval, don't give it access to any names. >> >>> [...] >> os.system("rm -rf *") >> Traceback (most recent call last): >> File "", line 1, in

Re: how to convert string to list or tuple

2005-05-29 Thread Duncan Booth
Dan Bishop wrote: > Simon Brunning wrote: >> [...] > > Or if you do use eval, don't give it access to any names. > >> [...] > os.system("rm -rf *") > Traceback (most recent call last): > File "", line 1, in ? > File "", line 0, in ? > NameError: name 'os' is not defined > Have you tried giv

Re: how to convert string to list or tuple

2005-05-29 Thread Dan Bishop
Simon Brunning wrote: > On 5/26/05, flyaflya <[EMAIL PROTECTED]> wrote: > > a = "(1,2,3)" > > I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', > > '2', ',', '3', ')') not (1,2,3) > > Short answer - use eval(). > > Long answer - *don't* use eval unless you are in control of the

Re: how to convert string to list or tuple

2005-05-29 Thread Steven D'Aprano
On Thu, 26 May 2005 19:53:38 +0800, flyaflya wrote: > a = "(1,2,3)" > I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', > '2', ',', '3', ')') not (1,2,3) Others have already given some suggestions. Here are some others. You didn't say where the input string a came from. Do y

Re: how to convert string to list or tuple

2005-05-26 Thread Fredrik Lundh
"flyaflya" <[EMAIL PROTECTED]> wrote: >a = "(1,2,3)" > I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', > '2', ',', '3', ')') not (1,2,3) if you trust the source, use eval(a) if you don't trust it, you can use, say tuple(int(x) for x in re.findall("\d+", a)) or, pe

Re: how to convert string to list or tuple

2005-05-26 Thread Simon Brunning
On 5/26/05, flyaflya <[EMAIL PROTECTED]> wrote: > a = "(1,2,3)" > I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', > '2', ',', '3', ')') not (1,2,3) Short answer - use eval(). Long answer - *don't* use eval unless you are in control of the source of the string that you are ev

how to convert string to list or tuple

2005-05-26 Thread flyaflya
a = "(1,2,3)" I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', '2', ',', '3', ')') not (1,2,3) -- http://mail.python.org/mailman/listinfo/python-list