How to Convert a String to a List in Python
To convert '['string']' to a list in Python, you can use the eval() function:
string_list = "['string']"
my_list = eval(string_list)
print(my_list)
Output:
['string']
However, using eval() can be dangerous if you are not sure about the contents of the string as it can execute arbitrary code. A safer way to convert this string to a list is by using the json module:
import json
string_list = "['string']"
my_list = json.loads(string_list)
print(my_list)
Output:
['string']
原文地址: https://www.cveoy.top/t/topic/mJtR 著作权归作者所有。请勿转载和采集!