Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #16454 > unrolled thread

Re: How convert a list string to a real list

Started byTerry Reedy <tjreedy@udel.edu>
First post2011-11-30 17:12 -0500
Last post2011-12-01 15:33 +1100
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: How convert a list string to a real list Terry Reedy <tjreedy@udel.edu> - 2011-11-30 17:12 -0500
    Re: How convert a list string to a real list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-01 03:42 +0000
      Re: How convert a list string to a real list Ben Finney <ben+python@benfinney.id.au> - 2011-12-01 15:25 +1100
        Re: How convert a list string to a real list Alec Taylor <alec.taylor6@gmail.com> - 2011-12-01 15:33 +1100

#16454 — Re: How convert a list string to a real list

FromTerry Reedy <tjreedy@udel.edu>
Date2011-11-30 17:12 -0500
SubjectRe: How convert a list string to a real list
Message-ID<mailman.3175.1322691152.27778.python-list@python.org>
On 11/30/2011 3:58 AM, Peter Otten wrote:
> Terry Reedy wrote:
>
>> On 11/30/2011 1:20 AM, 郭军权 wrote:
>>> Good after
>>> I have a string liststr = '["aaaa","bbbb","ccc"]' ,and I need convert it
>>> to a list like list = ["aaaa","bbbb","ccc"],what can id do?
>>
>> The easiest -- and most dangerous -- way is
>>   >>>  eval('["aaaa","bbbb","ccc"]')
>> ['aaaa', 'bbbb', 'ccc']
>>
>> But DO NOT eval unexamined strings from untrusted sources. The reason is
>> that it is much the same as letting an untrusted person sit unsupervised
>> as the keyboard of your computer with a command window open. You would
>> not want to eval
>>     "from os import system; system('<command to delete files>')"
>> where '<command...>' is replaced by something obnoxious for your
>> operating system.
>
> You can avoid these problems with ast.literal_eval():
>
> literal_eval(node_or_string)
>      Safely evaluate an expression node or a string containing a Python
>      expression.  The string or node provided may only consist of the
>      following Python literal structures: strings, numbers, tuples, lists,
>      dicts, booleans, and None.

I keep forgetting that someone thought to solve the problem of eval 
being both convinient and dangerous. Maybe if I type it once, I will 
remember.
 >>> import ast
 >>> ast.literal_eval('["aaaa","bbbb","ccc"]')
['aaaa', 'bbbb', 'ccc']

I think it would be better if safe_eval were available as an easily 
accessible builtin and dangerous_eval were tucked away in a module ;-).

-- 
Terry Jan Reedy

[toc] | [next] | [standalone]


#16469

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-12-01 03:42 +0000
Message-ID<4ed6f7be$0$29986$c3e8da3$5496439d@news.astraweb.com>
In reply to#16454
On Wed, 30 Nov 2011 17:12:10 -0500, Terry Reedy wrote:

> I think it would be better if safe_eval were available as an easily
> accessible builtin and dangerous_eval were tucked away in a module ;-).

+100000



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#16472

FromBen Finney <ben+python@benfinney.id.au>
Date2011-12-01 15:25 +1100
Message-ID<87ehwo5364.fsf@benfinney.id.au>
In reply to#16469
Steven D'Aprano <steve+comp.lang.python@pearwood.info> writes:

> On Wed, 30 Nov 2011 17:12:10 -0500, Terry Reedy wrote:
>
> > I think it would be better if safe_eval were available as an easily
> > accessible builtin and dangerous_eval were tucked away in a module ;-).
>
> +100000

You do realise that any vote outside the range −1 through +1 is invalid,
right? Every person gets a maximum of 1, positive or negative. Outside
that, the vote police come to kick you off the internet.

-- 
 \                             “Everything is futile.” —Marvin of Borg |
  `\                                                                   |
_o__)                                                                  |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#16473

FromAlec Taylor <alec.taylor6@gmail.com>
Date2011-12-01 15:33 +1100
Message-ID<mailman.3187.1322714034.27778.python-list@python.org>
In reply to#16472
Dammit, been awake too long researching on the Internet, but I finally
reached the Last Page

On Thu, Dec 1, 2011 at 3:25 PM, Ben Finney <ben+python@benfinney.id.au> wrote:
> Steven D'Aprano <steve+comp.lang.python@pearwood.info> writes:
>
>> On Wed, 30 Nov 2011 17:12:10 -0500, Terry Reedy wrote:
>>
>> > I think it would be better if safe_eval were available as an easily
>> > accessible builtin and dangerous_eval were tucked away in a module ;-).
>>
>> +100000
>
> You do realise that any vote outside the range -1 through +1 is invalid,
> right? Every person gets a maximum of 1, positive or negative. Outside
> that, the vote police come to kick you off the internet.
>
> --
>  \                             "Everything is futile." --Marvin of Borg |
>  `\                                                                   |
> _o__)                                                                  |
> Ben Finney
> --
> http://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web