l = "['Hello', 'my', 'name', 'is', 'Apple']"
l1 = ['Hello', 'my', 'name', 'is', 'Apple']
type(l)
回报str
但我希望它是一个列表,如l1
is.
如何将该字符串转换为通用列表?
the ast
模块有一个literal_eval
这就是你想要的
import ast
l = "['Hello', 'my', 'name', 'is', 'Apple']"
l1 = ast.literal_eval(l)
Outputs:
['Hello', 'my', 'name', 'is', 'Apple']
docs
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)