convert string to list
Wolfram Kraus
kraus at hagen-partner.de
Wed Oct 13 11:09:54 EDT 2004
Wolfram Kraus wrote:
> 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"]
>>
>> Thanks you,
>> Jochen
>
> If you are sure that the list always looks that way:
>
> >>> l = '["A","B","C"] '
> >>> l[1:-2].replace('"','').split(',')
> ['A', 'B', 'C']
>
>
> HTH,
> Wolfram
Damn, I knew there must be something wrong with the -2 (too much work
today??):
>>> l = '["A","B","C"]'
>>> l[1:-1].replace('"','').split(',')
['A', 'B', 'C']
Or try the other solution with multiple arguments (better way IMHO)
Wolfram
More information about the Python-list
mailing list