<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
  <title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Cliff Wells wrote:<br>
<blockquote
 cite="mid1097669701.12092.62.camel@devilbox.devilnet.internal"
 type="cite">
  <pre wrap="">On Wed, 2004-10-13 at 14:03 +0200, Jochen Hub wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">Hi,

I am a real beginner in Python and I was wondering if there is a way to 
convert a string (which contains a list) to a "real" list.

I need this since I would like to give a list as an argument to my 
Python script from the bash:

#!/usr/bin/python
import sys
argument1 = sys.argv[1]
thelist = ???(argument1)

and then call the script from the bash like this

thescript.py ["A","B","C"]
    </pre>
  </blockquote>
  <pre wrap=""><!---->
You can use eval for this.  Better, you could simply pass your list as a
series of arguments from bash:

#!/bin/bash
myscript.py A B C


#!/usr/bin/python
import sys
thelist = sys.argv[1:]

Regards,
Cliff

  </pre>
</blockquote>
As Cliff and Diez stated, you're better off passing your list as
elements broken up and using sys.argv to get at them.  But, here is a
sample of how to use eval:<br>
<br>
<font face="Courier New, Courier, monospace">#!/usr/bin/env python<br>
<br>
import sys<br>
import string<br>
<br>
print "sys.argv>>", sys.argv<br>
if len(sys.argv) > 1:<br>
    s = "".join(sys.argv[1:])<br>
    l = eval(s)<br>
    for i, e in enumerate(l):<br>
        print i, e<br>
</font><br>
<br>
A problem with this approach is, you need to make sure you put your
list string in quotes like this:<br>
<br>
<font face="Courier New, Courier, monospace">./test_list.py "['a', 'b',
'c']"</font><br>
<br>
so that the list is treated by the shell as a single argument rather
than  a series of strings.  And, actually, the "[" character is
actually an executable (on *NIX operatins systems) that performs all
kinds of "tests" So, I don't think the shell will substitute it for
anything, but......you may not want to just leave it out unquoted..... 
Plus, if you don't quote the whole thing, your shell may gobble up your
quote marks and you won't get exactly what you're looking for.  Look at
your sys.argv.  The quotes around the letters are gone, gobbled up by
the shell:<br>
<font face="Courier New, Courier, monospace"><br>
[jmjones@qatestrunner python]$ ./test_list.py ['a','b','c']<br>
sys.argv>> ['./test_list.py', '[a,b,c]']<br>
Traceback (most recent call last):<br>
  File "./test_list.py", line 12, in ?<br>
    l = eval(s)<br>
  File "<string>", line 0, in ?<br>
NameError: name 'a' is not defined<br>
</font><br>
<br>
So, again, you're probably best to go with the solution proposed by
Cliff and Diez.<br>
<br>
<br>
Jeremy Jones<br>
</body>
</html>