-4

我正在从文件中读取并具有以下字符串

my_item = ['玛丽亚','何塞']

我怎样才能把它变成一个包含两个项目的列表,即

my_list = [玛丽亚,何塞]

4

1 回答 1

1

I am not quite sure I understand your question correctly, looks like you already have a list my_item, which is a list of two strings, string1 - 'maria' and string2 - 'jose'.

If you mean the complete string is something looks like: """my_item = ['maria','jose']""" Then you do something like this below:

inputString = "my_item = ['maria','jose']"

# value is a list type 
value = eval(inputString.split("=")[1])
# key is a string type
key = inputString.split("=")[0].strip()

# I don't think you can define a variable name while the script is running. 
# but you can use dictionary type to call it.
mydict = {}
mydict[key] = value

Then you can call mydict[key] to pick up the value which you want to lookup.

>>> print mydict['my_item'] 
['maria', 'jose']
于 2013-08-15T01:17:14.633 回答