Как убрать кавычки у каждого элемента списка?
У меня есть список:
['{"type": "Feature", "properties": {"area": 1}, "geometry": {"type": "Polygon", "coordinates": [[[0.0002700000000000001, 90.00026999999932],[0.0005400000000000066, 90.00026999999932],[0.0002700000000000001, 90.00053999999865],[0.0005400000000000066, 90.00053999999865]]]}}',
'{"type": "Feature", "properties": {"area": 2}, "geometry": {"type": "Polygon", "coordinates": [[[0.0002700000000000001, 90.00053999999865],[0.0005400000000000066, 90.00053999999865],[0.0002700000000000001, 90.00080999999795],[0.0005400000000000066, 90.00080999999795]]]}}',
'{"type": "Feature", "properties": {"area": 3}, "geometry": {"type": "Polygon", "coordinates": [[[0.0002700000000000001, 90.00080999999795],[0.0005400000000000066, 90.00080999999795],[0.0002700000000000001, 90.00107999999727],[0.0005400000000000066, 90.00107999999727]]]}}']
Весь список мне нужно преобразовать в JSON. Для этого хочу превратить список вот такой формат:
[{"type": "Feature", "properties": {"area": 1}, "geometry": {"type": "Polygon", "coordinates": [[[0.0002700000000000001, 90.00026999999932],[0.0005400000000000066, 90.00026999999932],[0.0002700000000000001, 90.00053999999865],[0.0005400000000000066, 90.00053999999865]]]}},
{"type": "Feature", "properties": {"area": 2}, "geometry": {"type": "Polygon", "coordinates": [[[0.0002700000000000001, 90.00053999999865],[0.0005400000000000066, 90.00053999999865],[0.0002700000000000001, 90.00080999999795],[0.0005400000000000066, 90.00080999999795]]]}},
{"type": "Feature", "properties": {"area": 3}, "geometry": {"type": "Polygon", "coordinates": [[[0.0002700000000000001, 90.00080999999795],[0.0005400000000000066, 90.00080999999795],[0.0002700000000000001, 90.00107999999727],[0.0005400000000000066, 90.00107999999727]]]}}]
Т. е. убрать кавычки у каждого элемента списка.
Как это можно сделать?
Ответы (1 шт):
Автор решения: MaxU
→ Ссылка
воспользуйтесь json.loads() чтобы распарсить каждую строку списка в JSON объект (dictionary):
import json
res = [json.loads(s) for s in lst]
результат:
In [33]: res
Out[33]:
[{'type': 'Feature',
'properties': {'area': 1},
'geometry': {'type': 'Polygon',
'coordinates': [[[0.0002700000000000001, 90.00026999999932],
[0.0005400000000000066, 90.00026999999932],
[0.0002700000000000001, 90.00053999999865],
[0.0005400000000000066, 90.00053999999865]]]}},
{'type': 'Feature',
'properties': {'area': 2},
'geometry': {'type': 'Polygon',
'coordinates': [[[0.0002700000000000001, 90.00053999999865],
[0.0005400000000000066, 90.00053999999865],
[0.0002700000000000001, 90.00080999999795],
[0.0005400000000000066, 90.00080999999795]]]}},
{'type': 'Feature',
'properties': {'area': 3},
'geometry': {'type': 'Polygon',
'coordinates': [[[0.0002700000000000001, 90.00080999999795],
[0.0005400000000000066, 90.00080999999795],
[0.0002700000000000001, 90.00107999999727],
[0.0005400000000000066, 90.00107999999727]]]}}]