替换Python中JSON文件的多个键和值

2024-03-28 09:31:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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

{
    "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的以下操作:

  1. 将密钥EntityHandle替换为Name
  2. 将十六进制数中EntityHandle的值替换为'sf_001', 'sf_002', 'sf_003',依此类推
  3. type的值LineString替换为Polygon
  4. coordinates的两个方括号替换为三个方括号。在

预期输出:

^{pr2}$

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

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'

Tags: 文件nameindatageojsontypepropertiessf
1条回答
网友
1楼 · 发布于 2024-03-28 09:31:03

试试看:

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

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

要替换一个值,只需将新值设置为特定键:

^{pr2}$

完整代码:

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

相关问题 更多 >