How convert a list string to a real list

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

How convert a list string to a real list

郭军权
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? 
Thanks.


--
郭军权
清华大学网络中心网络安全实验室
guojunquan{at}gmail.com


--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: How convert a list string to a real list

Alec Taylor
import json

s = json.dumps([1, 2, 3, 4])
# '[1, 2, 3, 4]'
l = json.loads(s)
# [1, 2, 3, 4]

2011/11/30 郭军权 <[hidden email]>:

> 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?
> Thanks.
>
>
> --
> 郭军权
> 清华大学网络中心网络安全实验室
> guojunquan{at}gmail.com
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: How convert a list string to a real list

Terry Reedy
In reply to this post by 郭军权
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.

--
Terry Jan Reedy


--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: How convert a list string to a real list

Arnaud Delobelle-3
In reply to this post by 郭军权


On Nov 30, 2011 6:21 AM, "郭军权" <[hidden email]> 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? 
> Thanks.
>

Look up the json module.

--
Arnaud


--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: How convert a list string to a real list

Alec Taylor
Arnaud: Already showed that solution

On Wed, Nov 30, 2011 at 6:09 PM, Arnaud Delobelle <[hidden email]> wrote:

>
> On Nov 30, 2011 6:21 AM, "郭军权" <[hidden email]> 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?
>> Thanks.
>>
>
> Look up the json module.
>
> --
> Arnaud
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: How convert a list string to a real list

Peter Otten
In reply to this post by Terry Reedy
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.


--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: How convert a list string to a real list

Terry Reedy
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


--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: How convert a list string to a real list

hidura

Why you don't make this "['1','2','3']".strip("[]").split(',') work for me

El nov 30, 2011 10:16 p.m., "Terry Reedy" <[hidden email]> escribió:
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


--
http://mail.python.org/mailman/listinfo/python-list

--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: How convert a list string to a real list

Terry Reedy


On 11/30/2011 5:48 PM, Hidura wrote:
> Why you don't make this "['1','2','3']".strip("[]").split(',') work for me

Look more carefully. This is not the same as ast.literal_eval().

 >>> "['1','2','3']".strip("[]").split(',')
["'1'", "'2'", "'3'"]  # list of 3-char strings
 >>> ast.literal_eval("['1','2','3']")
['1', '2', '3']  # list of 1-char strings

Even if it were the same, it would be specific to lists of strings and
would not work for anything else.
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: How convert a list string to a real list

TIm Chase-3
In reply to this post by hidura
On 11/30/11 16:48, Hidura wrote:
> Why you don't make this "['1','2','3']".strip("[]").split(',') work for me

because it breaks on things like

   s = """
     [[1,2,3],42,'''triple the fun!''', "can't touch this,
eh?",r'"Back\\\slashes?!", she said.', [4,5,6]]
     """

Commas can be embedded in strings, strings can be delimited with
single, double, or triple quotes, raw strings can be used, more
than one opening or closing "["/"]" can appear in a row, leading
or trailing whitespace might throw you off...using
ast.literal_eval() should just Do the Right Thing™.

-tkc


--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: How convert a list string to a real list

Steven D'Aprano-11
In reply to this post by Peter Otten
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
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: How convert a list string to a real list

Ben Finney-10
Steven D'Aprano <[hidden email]> 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
Reply | Threaded
Open this post in threaded view
|

Re: How convert a list string to a real list

Alec Taylor
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 <[hidden email]> wrote:

> Steven D'Aprano <[hidden email]> 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
--
http://mail.python.org/mailman/listinfo/python-list