Home > Net >  Updating dictionary in Python
Updating dictionary in Python

Time:11-01

I'm working on a project where I will retrieve data from various files and this data will then be written down in a file in geojson format.

Below you see a simplified example of some of the code and output:

Code:

def get_data(data):

    features = {
        "type": "FeatureCollection",
        "features": [
            {
                "type": "Feature",
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        data["lat"],
                        data["long"],
                    ],
                },
                "properties": {
                    "obsid": data["file_name"],
                    "name": data["guid_id"],
                    "h_gs": data["z"],
                },
            }
        ],
    }
    for id, top, bot, code in zip(
        data["id"],
        data["top"],
        data["bot"],
        data["code"],
    ):

        info = {
            id: {
                "top": top,
                "bot": bot,
                "code": code,
            },
        }

        features["features"].append(info)

    return features


def main(data):
    data = get_data(data)
    to_json = json.dumps(data, indent=4)
    print(to_json)


if __name__ == "__main__":

    # example data
    data = {
        "lat": 40.730610,
        "long": -73.935242,
        "z": 28.37,
        "file_name": "tmrx.txt",
        "guid_id": "d4d5b10a-c5fc-450a-9b3b-f309e7cb9613",
        "id": ["id_0", "id_1", "id_2", "id_3", "id_4"],
        "top": [100, 200, 300, 400, 500],
        "bot": [90, 190, 290, 390, 490],
        "code": ["a", "b", "c", "d", "e"],
    }

    main(data)

Output:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    40.73061,
                    -73.935242
                ]
            },
            "properties": {
                "obsid": "tmrx.txt",
                "name": "d4d5b10a-c5fc-450a-9b3b-f309e7cb9613",
                "h_gs": 28.37
            }
        },
        {
            "id_0": {
                "top": 100,
                "bot": 90,
                "code": "a"
            }
        },
        {
            "id_1": {
                "top": 200,
                "bot": 190,
                "code": "b"
            }
        },
        {
            "id_2": {
                "top": 300,
                "bot": 290,
                "code": "c"
            }
        },
        {
            "id_3": {
                "top": 400,
                "bot": 390,
                "code": "d"
            }
        },
        {
            "id_4": {
                "top": 500,
                "bot": 490,
                "code": "e"
            }
        }
    ]
}

This works fine but I wish I could get the output to look a little different.

Desired output:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    40.73061,
                    -73.935242
                ]
            },
            "properties": {
                "obsid": "tmrx.txt",
                "name": "d4d5b10a-c5fc-450a-9b3b-f309e7cb9613",
                "h_gs": 28.37
            }
            "id_0": {
                "top": 100,
                "bot": 90,
                "code": "a"
            }
            "id_1": {
                "top": 200,
                "bot": 190,
                "code": "b"
            }
            "id_2": {
                "top": 300,
                "bot": 290,
                "code": "c"
            }
            "id_3": {
                "top": 400,
                "bot": 390,
                "code": "d"
            }
            "id_4": {
                "top": 500,
                "bot": 490,
                "code": "e"
            }
        }
    ]
}

There, the 'unnecessary' brackets are removed.

Does anyone here on the forum know how i could achieve this result in the above code?

Best Regards, Mikael

See the code sample above.

CodePudding user response:

Instead of features["features"].append(info) use:

features["features"][0].update(info)

CodePudding user response:

Change below Line in your code

features["features"].append(info)

To this

features["features"][0] |= info
  • Related