Python - 使用 json 导入 geojson 并打印特定数据

Python - Using json to import geojson and printing specific data

import json 

with open("madagascar.geojson") as f:
    data = json.load(f)

我完全不知道该去哪里。这是我生成的所有代码,因为我是 python 的新手。我有一个马达加斯加 geojson 文件,我正在尝试使用 json 读取。完成后,我想打印出数据集中的第 5 个区域,使用的短语大意为““第五条记录用于 X,其面积为 Y 平方公里。” X 是区域名称,Y 是区域。

我该怎么做?

GeoJSON 文本文件:https://drive.google.com/file/d/1we40zpuGQICfyTw7YtJ39OY7gtPUQJr7/view?usp=sharing

此代码打印所有条目的名称、区域和面积:

import json 

with open("madagascar.geojson") as f:
    data = json.load(f)

for i in range(len(data["features"])):
    properties = data["features"][i]["properties"]
    print("Record %d: Name %s, Region %s, Area %s sq. km" 
        % (i,properties ["gn_name"], properties ["region_sub"], properties ["areasqkm"]))

这是输出:

为您的最后一个问题创建答案以便能够 post 一段代码:

import json
with open("Madagascar.txt") as input_file:
    data = json.load(input_file)
    for feature in data["features"]:
        props = feature["properties"]
        print("Name %s, region %s, area %s" % (props["gn_name"], props["region_sub"], props["areasqkm"]))

如果您刚开始学习 Python,请查看 Dive into Python,这是一个很棒的教程。