convert string to list
kraus at hagen-partner.de
Wed Oct 13 17:09:54 CEST 2004
Wolfram Kraus wrote:
> Jochen Hub wrote:
>> 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:
>> import sys
>> argument1 = sys.argv
>> thelist = ???(argument1)
>> and then call the script from the bash like this
>> thescript.py ["A","B","C"]
>> Thanks you,
> If you are sure that the list always looks that way:
> >>> l = '["A","B","C"] '
> >>> l[1:-2].replace('"','').split(',')
> ['A', 'B', 'C']
Damn, I knew there must be something wrong with the -2 (too much work
>>> l = '["A","B","C"]'
['A', 'B', 'C']
Or try the other solution with multiple arguments (better way IMHO)
More information about the Python-list