Python convert formatted string to list
I have a string "[u'foo']"
(Yes, it includes the square brackets and the u''
). I have to convert开发者_如何学C that to a list which looks like [u'foo']
.
list("[u'foo']")
won't work.
Any suggestions?
>>> import ast
>>> s = "[u'foo']"
>>> ast.literal_eval(s)
[u'foo']
documentation
eval("[u'foo']", {'__builtins__':[]}, {})
精彩评论