Python将格式化的字符串转换为列表 [英] Python convert formatted string to list
本文介绍了Python将格式化的字符串转换为列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串"[u'foo']"
(是的,它包括方括号和u''
).我必须将其转换为类似于[u'foo']
的列表.
I have a string "[u'foo']"
(Yes, it includes the square brackets and the u''
). I have to convert that to a list which looks like [u'foo']
.
list("[u'foo']")
无法正常工作.
有什么建议吗?
推荐答案
>>> import ast
>>> s = "[u'foo']"
>>> ast.literal_eval(s)
[u'foo']
这篇关于Python将格式化的字符串转换为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文