On Wed, Jul 11, 2012 at 3:30 PM, <tutor-requ...@python.org> wrote: > Send Tutor mailing list submissions to > tutor@python.org > > To subscribe or unsubscribe via the World Wide Web, visit > http://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to > tutor-requ...@python.org > > You can reach the person managing the list at > tutor-ow...@python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > > 1. Re: Extracting the list from a string (Steven D'Aprano) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Wed, 11 Jul 2012 19:10:19 +1000 > From: Steven D'Aprano <st...@pearwood.info> > To: tutor@python.org > Subject: Re: [Tutor] Extracting the list from a string > Message-ID: <20120711091018.GA21489@ando> > Content-Type: text/plain; charset=us-ascii > > On Wed, Jul 11, 2012 at 02:18:04PM +0530, kala Vinay wrote: > > Hi all, > > > > say i have a string s="['a','b']" and i want to get a list object > > from this string s ie l=['a','b'] > > The AST module contains a safe way to parse and eval literal > expressions, including strings, nested lists, and builtin constants like > None, True and False. > > py> import ast > py> ast.literal_eval('[23, 42, "hello", "world", -1.5, [], {}]') > [23, 42, 'hello', 'world', None, True, False, -1.5, [], {}] > > Unlike the eval command, it is safe and won't execute code: > > py> text = '__import__("os").system("echo \\"Your computer is mine > now!\\"")') > py> eval(text) > Your computer is mine now! > 0 > py> ast.literal_eval(text) > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > File "/usr/lib/python2.6/ast.py", line 68, in literal_eval > return _convert(node_or_string) > File "/usr/lib/python2.6/ast.py", line 67, in _convert > raise ValueError('malformed string') > ValueError: malformed string > > > -- > Steven >
> Thank You Steven, Its working fine.... > > > ------------------------------ > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > > End of Tutor Digest, Vol 101, Issue 37 > ************************************** > -- Regards, Kala
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor