美文网首页
Convert string represent list to

Convert string represent list to

作者: 阿o醒 | 来源:发表于2017-08-13 10:40 被阅读3次

"ABC"转成 list 格式只要list("ABC")就会得到["A", "B", "C"]
但如果需要把string格式的list转换成list呢?
如把

x = u'[ "A","B","C" , " D"]'

转成

x = ["A", "B", "C", "D"] 

ast

>>> import ast
>>> x = u'[ "A","B","C" , " D"]'
>>> x = ast.literal_eval(x)
>>> x
['A', 'B', 'C', ' D']
>>> x = [n.strip() for n in x]
>>> x
['A', 'B', 'C', 'D']

相关文章

网友评论

      本文标题:Convert string represent list to

      本文链接:https://www.haomeiwen.com/subject/etwsrxtx.html