Home > other >  extract coordinates from string key value
extract coordinates from string key value

Time:10-11

'{"name": "Polygon Sample", "geo_json": {"type": "Feature", "properties": {}, "geometry": {"type": "Polygon", "coordinates": [[[-121.1958, 37.6683], [-121.1779, 37.6687], [-121.1773, 37.6792], [-121.1958, 37.6792], [-121.1958, 37.668]]]}}}' this is one column value from a csv file, which i'm getting as string. I need to extract only coordinates like -> [[-121.1958, 37.6683], [-121.1779, 37.6687], [-121.1773, 37.6792], [-121.1958, 37.6792], [-121.1958, 37.668]]. How can i solve this? With eval() how can i do that?

CodePudding user response:

In python you can covert the string to dict using json library and then access coordinates

import json
input_data = '{"name": "Polygon Sample", "geo_json": {"type": "Feature", "properties": {}, "geometry": {"type": "Polygon", "coordinates": [[[-121.1958, 37.6683], [-121.1779, 37.6687], [-121.1773, 37.6792], [-121.1958, 37.6792], [-121.1958, 37.668]]]}}}'
data_dict = json.loads(input_data)
print(data_dict["geo_json"]["geometry"]["coordinates"])
  • Related