-4

ファイルから読み取っていて、次の文字列があります

my_item = ['マリア' , 'ホセ']

これを2つのアイテムを持つリストにするにはどうすればよいですか

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 に答える