使用 Python 将元素附加到 json dict (geojson)

Append element to json dict (geojson) using Python

我想通过 Python 为我的 geojson 添加样式。当前功能目前没有任何样式元素。我想附加样式然后填充。但是,当我这样做时,文件中没有添加任何内容。和以前一样

import json

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

for feature in data['features']:
    feature.append("style")
    feature["style"].append({"fill":color})

示例 GeoJson

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },

"features": [
{ "type": "Feature", "properties": { "STATEFP": "17", "COUNTYFP": "019", "TRACTCE": "005401", "BLKGRPCE": "2", "GEOID": "170190054012", "NAMELSAD": "Block Group 2", "MTFCC": "G5030", "FUNCSTAT": "S", "ALAND": 574246.000000, "AWATER": 4116.000000, "INTPTLAT": "+40.1238204", "INTPTLON": "-088.2038105", "GISJOIN": "G17001900054012", "STUSPS": "IL", "SHAPE_AREA": 578361.706954, "SHAPE_LEN": 3489.996273, "census_block_income_YEAR": "2009-2013", "census_block_income_STATE": "Illinois", "census_block_income_STATEA": 17, "census_block_income_COUNTY": "Champaign County"}}]}

我试图让最终结果成为:

    {
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },

"features": [
{ "type": "Feature", "properties": { "STATEFP": "17", "COUNTYFP": "019", "TRACTCE": "005401", "BLKGRPCE": "2", "GEOID": "170190054012", "NAMELSAD": "Block Group 2", "MTFCC": "G5030", "FUNCSTAT": "S", "ALAND": 574246.000000, "AWATER": 4116.000000, "INTPTLAT": "+40.1238204", "INTPTLON": "-088.2038105", "GISJOIN": "G17001900054012", "STUSPS": "IL", "SHAPE_AREA": 578361.706954, "SHAPE_LEN": 3489.996273, "census_block_income_YEAR": "2009-2013", "census_block_income_STATE": "Illinois", "census_block_income_STATEA": 17, "census_block_income_COUNTY": "Champaign County"},"style"{fill:"red"}}]}

您正在使用此处的词典列表,词典没有方法 append,您可以像这里一样创建新键:

for feature in data['features']:
    feature["style"] = {"fill":color}

似乎您需要使用 JSON:

重写文件
with open('test.json', 'w') as f:
    json.dump(data, f)

您永远不会将更改写回到文件中。将以下内容添加到代码末尾:

with open('test.json','w') as f:
    json.dump(data, f)

当您输入时

for feature in data['features']:

每个 feature 都是 data['features'] 列表中的一个项目。每个项目都有一个字典,所以你调用了错误的方法(append 是列表的方法)。

你可以写

for feature in data['features']:
    feature.update({"style": {"fill": "red"}})

最后,如果您想要更改从中获得初始 json 结构的文件,请确保将现在更新的数据结构写回到文件中:

with open('output2.json', 'w') as f:
    json.dump(data, f)

字典中没有append方法。应该使用 update.

import pprint as pp
for feature in data['features']:
    feature.update({'style':{'fill': 'red'}})

pp.pprint(data)

输出:

{'crs': {'properties': {'name': 'urn:ogc:def:crs:OGC:1.3:CRS84'},
         'type': 'name'},
 'features': [{'properties': {'ALAND': 574246.0,
                              'AWATER': 4116.0,
                              'BLKGRPCE': '2',
                              'COUNTYFP': '019',
                              'FUNCSTAT': 'S',
                              'GEOID': '170190054012',
                              'GISJOIN': 'G17001900054012',
                              'INTPTLAT': '+40.1238204',
                              'INTPTLON': '-088.2038105',
                              'MTFCC': 'G5030',
                              'NAMELSAD': 'Block Group 2',
                              'SHAPE_AREA': 578361.706954,
                              'SHAPE_LEN': 3489.996273,
                              'STATEFP': '17',
                              'STUSPS': 'IL',
                              'TRACTCE': '005401',
                              'census_block_income_COUNTY': 'Champaign County',
                              'census_block_income_STATE': 'Illinois',
                              'census_block_income_STATEA': 17,
                              'census_block_income_YEAR': '2009-2013'},
               'style': {'fill': 'red'},
               'type': 'Feature'}],
 'type': 'FeatureCollection'}