将字符串'[1,2,3,4]'转化为列表[1,2,3,4]
import json
import ast
k = '[1,2,3,4]' #str
k1 = json.loads(k)
print(k1,type(k1))
k2 = ast.literal_eval(k)
print(k2,type(k2))
k3 = eval(k)
print(k3,type(k3))
# 结果
# [1, 2, 3, 4] <class 'list'>
# [1, 2, 3, 4] <class 'list'>
# [1, 2, 3, 4] <class 'list'>
网友评论