https://blog.csdn.net/weixin_45912307/article/details/115985600
1. python列表的字符串形式转列表形式
- 方式1:
python内置函数eval()
- 方式2:
literal_eval
- 方式3:
json.loads()
- 方式4:
str.strip("[]").split(",")
2. 示例代码
# -- encoding: utf-8 --
# @time: 2021/4/18 12:33
# @Author: jsonLiu
# @Email: xxxxxxxxx@qq.com
# @file: listStrToList
import json
from ast import literal_eval
str1 = '["S-20210421-123","S-20210421-456","S-20210421-789"]'
#str = '['S-20210421-123','S-20210421-456','S-20210421-789']' # 必须是'""'或"''",否则报invalid syntax
str2 = '[S-20210421-123,S-20210421-456,S-20210421-789]'
# 方式1:python内置函数:eval
l1 = eval(str1)
print("type:%s%s" %(type(l1),l1))
# 方式2:json.loads()
l2 = json.loads(str1)
print("type:%s%s" %(type(l2),l2))
# 方式3:literal_eval
l3 = literal_eval(str1)
print("type:%s%s" %(type(l3),l3))
# 方式4:字符串拼接
l4 = str2.strip('[]').split(',')
print("type:%s%s" %(type(l4),l4))
3. 最终运行结果