[Tutor] Tutor Digest, Vol 101, Issue 37

kala Vinay kala586 at gmail.com
Wed Jul 11 12:09:52 CEST 2012


On Wed, Jul 11, 2012 at 3:30 PM, <tutor-request at python.org> wrote:

> Send Tutor mailing list submissions to
>         tutor at 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-request at python.org
>
> You can reach the person managing the list at
>         tutor-owner at 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 <steve at pearwood.info>
> To: tutor at python.org
> Subject: Re: [Tutor] Extracting the list from a string
> Message-ID: <20120711091018.GA21489 at 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 at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> End of Tutor Digest, Vol 101, Issue 37
> **************************************
>



-- 
Regards,
Kala
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120711/ed72df06/attachment.html>


More information about the Tutor mailing list