如何将字符串转换为列表?
说的字符串就像text = "a,b,c"。转换后,text == ['a', 'b', 'c']希望是text[0] == 'a',text[1] == 'b'?
text = "a,b,c"
text == ['a', 'b', 'c']
text[0] == 'a'
text[1] == 'b'
像这样:
>>> text = 'a,b,c' >>> text = text.split(',') >>> text [ 'a', 'b', 'c' ]
另外,eval()如果您信任字符串是安全的,则可以使用:
eval()
>>> text = 'a,b,c' >>> text = eval('[' + text + ']')