convert string to list

Cliff Wells clifford.wells at comcast.net
Wed Oct 13 08:15:01 EDT 2004


On Wed, 2004-10-13 at 14:03 +0200, Jochen Hub wrote:
> 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"]

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

-- 
Cliff Wells <clifford.wells at comcast.net>




More information about the Python-list mailing list