list_val = '["apple", "blue", "green", "orange", "cherry", "white", "red", "violet"]'
print type(list_var)
str
print type(list_var[0])
'['
我从文件中读取 list_val 值以及如何将 list_var 转换为列表?所以 list_var [0] 应该是 'apple'。
最佳答案
>>> list_val = '["apple", "blue", "green", "orange", "cherry", "white", "red", "violet"]'
>>>
>>> from ast import literal_eval
>>> list_val = literal_eval(list_val)
>>> list_val[0]
'apple'
关于python - 将字符串转换为列表 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33073821/