开发者

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__':[]}, {})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜