开发者

Python convert formatted string to list

开发者 https://www.devze.com 2023-01-13 21:45 出处:网络
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\'].

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

精彩评论

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