Python使用json导入geojson并打印特定的d

2024-10-01 15:47:12 发布

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

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


Tags: 文件代码importjson区域dataasgeojson
2条回答

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

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"]))

这是输出: enter image description here

为最后一个问题创建答案,以便能够发布代码块:

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 checkDive into Python,这是一个很好的教程。在

相关问题 更多 >

    热门问题