在Python中替换JSON文件的多个键和值 [英] Replace multiple keys and values of JSON file in Python

查看:171
本文介绍了在Python中替换JSON文件的多个键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于名为 data 的geojson类型文件,如下所示:

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]
                ]
            }
        }
    ]
}

我希望实现对 data 的以下操作:

I hope to realize the following manipulation to data:

  1. 将键 EntityHandle 替换为 Name ;
  2. 用十六进制数字'sf_001','sf_002','sf_003'等替换 EntityHandle 的值;
  3. type 的值 LineString 替换为 Polygon ;
  4. 用三个方括号替换 coordinates 的两个方括号.
  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.

这是预期的输出:

{
    "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]
                ] ]
            }
        }
    ]
}

我是使用Python在 JSON 文件操作中的新手.请帮助我,谢谢.

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']

结果:

Traceback (most recent call last):

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

KeyError: 'Name'

推荐答案

试一下:

您可以使用以下方法重命名密钥:

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

完整代码:

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'] ]]

这篇关于在Python中替换JSON文件的多个键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆