<分区>
我正在尝试将引号中包含的列表转换为列表。他们是执行此操作的最佳方法吗?
Ex: list = "[1,2,3,4,5]"
Operation : Convert list which is string to a list
o/p: list = [1,2,3,4,5]
<分区>
我正在尝试将引号中包含的列表转换为列表。他们是执行此操作的最佳方法吗?
Ex: list = "[1,2,3,4,5]"
Operation : Convert list which is string to a list
o/p: list = [1,2,3,4,5]
最佳答案
您可以使用 ast.literal_eval()
以安全地评估字符串:
Safely evaluate an expression node or a string containing a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None.
In [1]: from ast import literal_eval
In [2]: literal_eval("[1,2,3,4,5]")
Out[2]: [1, 2, 3, 4, 5]
关于python - 将字符串封闭列表转换为列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43752490/