filter = '[{"name":"MinPrice","value": "50"},{"name":"MaxPrice","value": "100"}]'
how can I convert this string to list? Not changing actual data.Data should not change
filter = '[{"name":"MinPrice","value": "50"},{"name":"MaxPrice","value": "100"}]'
how can I convert this string to list? Not changing actual data.Data should not change
First, please don't call it filter
,
as that shadows a very nice builtin identifier.
I will rename as filt
.
Use the json module:
>>> import json
>>>
>>> filt = '[{"name":"MinPrice","value": "50"},{"name":"MaxPrice","value": "100"}]'
>>> lst = json.loads(filt)
>>> len(lst)
2
>>> lst[0]
{'name': 'MinPrice', 'value': '50'}
>>>
>>> lst[1]
{'name': 'MaxPrice', 'value': '100'}
Try this:
>>> import ast
>>> f = '[{"name":"MinPrice","value": "50"},{"name":"MaxPrice","value": "100"}]'
>>> output = ast.literal_eval(f)
>>> type(output)
<class 'list'>