You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

遍历GeoJSON文件时遇TypeError:列表索引需为整数或切片而非字符串

解决GeoJSON遍历中的TypeError问题

错误信息

20     for feature in features:
     21         # Extract properties and geometry
---> 22         properties = feature['properties']
     23         geometry = feature['geometry']
     24 
TypeError: list indices must be integers or slices, not str.

问题中的GeoJSON结构

{"type":"FeatureCollection","name":"10","crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}},"features":[[{"type":"Feature","properties":{"Id":"75"},"geometry":{"type":"MultiPolygon","coordinates":[[[[["71.000348","31.199419"],["71.00038","31.198841"],["71.000294","31.198832"],["71.000294","31.197318"],["71.0006","31.197379"],["71.000712","31.197094"],["71.000718","31.196727"],["71.001088","31.19675"],["71.001329","31.196746"],["71.001426","31.196759"],["71.001442","31.197227"],["71.001463","31.197792"],["71.001409","31.198853"],["71.001356","31.199478"],["71.000348","31.199419"]]]]}},{"type":"Feature","properties":{"Id":"77"},"geometry":{"type":"MultiPolygon","coordinates":[[[[["72.469083","31.00806"],["72.469083","31.00806"],["72.470054","31.009012"],["72.470054","31.009012"],["72.471138","31.008148"],["72.471138","31.008148"],["72.47021","31.007159"],["72.47021","31.007159"],["72.469083","31.00806"]]]]}}]]}

原Python代码

import json
import glob
import geopandas as gpd
import geemap

# Specify the folder path containing GeoJSON files
folder_path = 'D:/Test'

# Get a list of all GeoJSON files in the folder
geojson_files = glob.glob(folder_path + '/*.geojson')

# Iterate over each GeoJSON file
for geojson_file in geojson_files:
    with open(geojson_file, 'r') as f:
        data = json.load(f)
    
    # Access the features in the GeoJSON file
    features = data['features']

    for feature in features:
        # Extract properties and geometry
        properties = feature['properties']
        geometry = feature['geometry']

        # Convert to GeoDataFrame
        nReserve_feature = gpd.GeoDataFrame.from_features([{
            'geometry': geometry,
            'properties': properties
        }])

        # Load the GeoJSON string into Earth Engine
        nReserve_geojson = geemap.gdf_to_ee(nReserve_feature, geodesic=False)

问题原因

你的GeoJSON文件中features字段是嵌套列表(外层列表里还有一层子列表),原代码直接遍历features得到的是子列表而非Feature对象,自然无法用字符串索引访问properties和geometry。

修正后的代码

方案1:多层循环兼容不同嵌套情况

如果不确定所有GeoJSON文件的嵌套层级是否一致,建议用两层循环遍历:

import json
import glob
import geopandas as gpd
import geemap

# Specify the folder path containing GeoJSON files
folder_path = 'D:/Test'

# Get a list of all GeoJSON files in the folder
geojson_files = glob.glob(folder_path + '/*.geojson')

# Iterate over each GeoJSON file
for geojson_file in geojson_files:
    with open(geojson_file, 'r') as f:
        data = json.load(f)
    
    # Access the features in the GeoJSON file
    features = data['features']

    # 先遍历外层列表的子列表
    for feature_list in features:
        # 再遍历子列表中的Feature对象
        for feature in feature_list:
            # Extract properties and geometry
            properties = feature['properties']
            geometry = feature['geometry']

            # Convert to GeoDataFrame
            nReserve_feature = gpd.GeoDataFrame.from_features([{
                'geometry': geometry,
                'properties': properties
            }])

            # Load the GeoJSON string into Earth Engine
            nReserve_geojson = geemap.gdf_to_ee(nReserve_feature, geodesic=False)

方案2:直接取内层列表(适合结构统一的情况)

如果确定所有GeoJSON文件的features都是两层嵌套,可以直接取出内层列表再遍历:

import json
import glob
import geopandas as gpd
import geemap

# Specify the folder path containing GeoJSON files
folder_path = 'D:/Test'

# Get a list of all GeoJSON files in the folder
geojson_files = glob.glob(folder_path + '/*.geojson')

# Iterate over each GeoJSON file
for geojson_file in geojson_files:
    with open(geojson_file, 'r') as f:
        data = json.load(f)
    
    # 直接取出内层的Feature列表
    features = data['features'][0]

    for feature in features:
        # Extract properties and geometry
        properties = feature['properties']
        geometry = feature['geometry']

        # Convert to GeoDataFrame
        nReserve_feature = gpd.GeoDataFrame.from_features([{
            'geometry': geometry,
            'properties': properties
        }])

        # Load the GeoJSON string into Earth Engine
        nReserve_geojson = geemap.gdf_to_ee(nReserve_feature, geodesic=False)

内容的提问来源于stack exchange,提问作者Muhammad Waqas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 07:27:49