expr_str="[1,2,3]"
my_list=eval(expr_str)
print(repr(my_list),type(my_list)) # [1,2,3]
print(repr(expr_str),type(expr_str)) #'[1,2,3]'
import ast # 用import ast 来代替以下这种操作eval可以提高速度
my_list=ast.literal_eval(expr_str)
print(my_list) #[1,2,3]
输出:
[1, 2, 3] <class 'list'>
'[1,2,3]' <class 'str'>
[1, 2, 3]
标签:eval,ast,python,expr,list,str,my
From: https://www.cnblogs.com/heris/p/17796085.html