使用预定义的键创建嵌套字典,但在 python 中没有值

Create nested dictionary with predefined keys but without values in python

瞄准

对于作业,我需要输入一些建筑信息几何图形及其属性 ) 转换为 GeoJSON 数据结构。

方法

我一般的做法是: 使用必要的 GeoJSON 数据结构和 .append 该结构的数据创建一个空字典 (见:下文)

output_buildings = {
    'type': "FeatureCollection",
    'features': [
    {
        'type': "Feature",
        'geometry': {
            'type': ,
            'coordinates': 
        },
        'properties': {
            'building_id': ,
            'building_year': ,
            'building_status': ,
            'building_occurance': 
        }
        }
    ]
}

问题

我知道如何创建一个简单的空字典,但我的问题是我不知道如何创建一个键结构没有值(因为我想稍后附加这些值)。我在 ,typefeatures 中的第二个)收到一个错误。

之前的努力

问题

我还没有找到解决办法。你能给我一些建议吗?

提前致谢!

您当前的字典结构无效,因为您必须存在 key-value 对。

您可以通过为没有指定值的键填写 None 占位符来解决这个问题。但是,键 coordinates 可以设置为空列表,因为您可以有多个坐标。

有效的 GeoJSON 结构:

output_buildings = {
    'type': "FeatureCollection",
    'features': [
        {
            'type': "Feature",
            'geometry': {
                'type': None,
                'coordinates': []
            },
            'properties': {
                'building_id': None,
                'building_year': None,
                'building_status': None,
                'building_occurance': None
            }
        }
    ]
}