[英]Convert String of List to List in Python
I need to convert a string of list to List in Python.我需要将列表字符串转换为 Python 中的列表。 I have seen many of the similar questions but none of them works in this case.我见过许多类似的问题,但在这种情况下它们都不起作用。
I am passing some values through PostMan.我正在通过 PostMan 传递一些值。
The key passing as a form data作为表单数据传递的键
Key = controls
value = [CR1,CR2]
I am fetching the data like this我正在获取这样的数据
c_list = self._kwargs['data'].get('controls', [])
print(c-list)
print(type(c-list))
I am getting the following o/p我得到以下 o/p
[CC-2,CC-3]
<class 'str'>
But I need to get it as a list so I have tried the following method但我需要把它作为一个列表,所以我尝试了以下方法
import ast
c_list = self._kwargs['data'].get('controls', [])
res = ast.literal_eval(c_list)
But I am getting the following Error但我收到以下错误
malformed node or string: <_ast.Name object at 0x7f82966942b0>
You could simply do the following: strip
the brackets and split
on the commas您可以简单地执行以下操作: strip
括号并用逗号split
>>> s = "[CC-2,CC-3]"
>>> s.strip('[]').split(',')
['CC-2', 'CC-3']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.