Replace multiple keys and values of JSON file in Python

狂风中的少年 提交于 2021-02-11 12:38:02

问题


For geojson type file named data as follows:

{
    "type": "FeatureCollection",
    "name": "entities",
    "features": [{
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "EntityHandle": "1A0"
            },
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [3220.136443006845184, 3001.530372177397112],
                    [3847.34171007254281, 3000.86074447018018],
                    [3847.34171007254281, 2785.240077064262096],
                    [3260.34191304818205, 2785.240077064262096],
                    [3260.34191304818205, 2795.954148466309107]
                ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "EntityHandle": "1A4"
            },
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [3611.469650131302842, 2846.845982610575902],
                    [3695.231030111376185, 2846.845982610575902],
                    [3695.231030111376185, 2785.240077064262096],
                    [3611.469650131302842, 2785.240077064262096],
                    [3611.469650131302842, 2846.845982610575902]
                ]
            }
        }
    ]
}

I hope to realize the following manipulation to data:

  1. replace key EntityHandle with Name;
  2. replace EntityHandle's value from hex number with 'sf_001', 'sf_002', 'sf_003', etc;
  3. replace type's value LineString with Polygon;
  4. replace coordinates's two square brackets with three square brackets.

This is expected output:

{
    "type": "FeatureCollection",
    "name": "entities",
    "features": [{
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "Name": "sf_001"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [ [
                    [3220.136443006845184, 3001.530372177397112],
                    [3847.34171007254281, 3000.86074447018018],
                    [3847.34171007254281, 2785.240077064262096],
                    [3260.34191304818205, 2785.240077064262096],
                    [3260.34191304818205, 2795.954148466309107]
                ] ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "Name": "sf_002"
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [ [
                    [3611.469650131302842, 2846.845982610575902],
                    [3695.231030111376185, 2846.845982610575902],
                    [3695.231030111376185, 2785.240077064262096],
                    [3611.469650131302842, 2785.240077064262096],
                    [3611.469650131302842, 2846.845982610575902]
                ] ]
            }
        }
    ]
}

I'm new in JSON file manipulation using Python. Please help me, thanks at advance.

import json
from pprint import pprint

with open('data.geojson') as f:
    data = json.load(f)

pprint(data)

for feature in data['features']:
    #print(feature)
    print(feature['properties']['EntityHandle'])

for feature in data['features']: 
    feature['properties']['EntityHandle'] = feature['properties']['Name'] #Rename `EntityHandle` to `Name`
    del feature['properties']['EntityHandle']

Result:

Traceback (most recent call last):

  File "<ipython-input-79-8b30b71fedf9>", line 2, in <module>
    feature['properties']['EntityHandle'] = feature['properties']['Name']

KeyError: 'Name'

回答1:


Give this a shot:

You can rename a key by using:

dict['NewKey'] = dict.pop('OldKey')

And to replace a value, it's just as simple as setting the new vale equal to the specific key:

dict['Key'] = new_value

Full Code:

data =  {
    "type": "FeatureCollection",
    "name": "entities",
    "features": [{
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "EntityHandle": "1A0"
            },
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [3220.136443006845184, 3001.530372177397112],
                    [3847.34171007254281, 3000.86074447018018],
                    [3847.34171007254281, 2785.240077064262096],
                    [3260.34191304818205, 2785.240077064262096],
                    [3260.34191304818205, 2795.954148466309107]
                ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "Layer": "0",
                "SubClasses": "AcDbEntity:AcDbPolyline",
                "EntityHandle": "1A4"
            },
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [3611.469650131302842, 2846.845982610575902],
                    [3695.231030111376185, 2846.845982610575902],
                    [3695.231030111376185, 2785.240077064262096],
                    [3611.469650131302842, 2785.240077064262096],
                    [3611.469650131302842, 2846.845982610575902]
                ]
            }
        }
    ]
}



sf_count = 0
for feature in data['features']: 
    feature['properties']['Name'] = feature['properties'].pop('EntityHandle') #Rename `EntityHandle` to `Name`

    sf_count += 1
    feature['properties']['Name'] = 'sf_%.3d' %sf_count # Replace hex with incrimental sf_xxx

    feature['geometry']['type'] = 'Polygon' # Replace `LineString` to `Polygon`

    feature['geometry']['coordinates'] = [[ each for each in feature['geometry']['coordinates'] ]]


来源:https://stackoverflow.com/questions/55394657/replace-multiple-keys-and-values-of-json-file-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!