【发布时间】:2018-10-06 00:15:46
【问题描述】:
如何使用 python 将以下字符串转换为数组(此字符串可能包含不定数量的项)?
'["Foo","Bar","Baz","Woo"]'
这绝对也是一个字符串表示。 type() 给了:
<class 'str'>
更新:
知道了。
interestedin = request.POST.get('interestedIn')[1:-1].split(',')
interested = []
for element in interestedin:
interested.append(element[1:-1])
其中request.POST.get('interestedIn') 给出了'["Foo","Bar","Baz","Woo"]' 字符串列表“事物”。
【问题讨论】:
-
这看起来像一个列表,而不是一个字符串...
-
你说的是这个列表里面的元素吗?
-
这看起来像是一个重复的问题——我相信我至少见过几个变种。无论如何,您尝试了什么?为什么它不起作用?
-
ast.literal_eval()或json.loads()...