【问题标题】:How to replace values in an attribute table for one column?如何替换属性表中一列的值?
【发布时间】:2016-04-21 04:10:30
【问题描述】:

我需要替换属性表中一列的值(将名为“label”的列中的零替换为 100)。这可能使用 ogr 或 python 吗?我必须为 500 多个 shapefile 执行此操作。

【问题讨论】:

  • 是的,您可以在 python 中轻松做到这一点。您尝试了什么或卡在哪里?您可以先查看os.walk() python 函数(以便遍历目录结构并获取文件名),然后您可以尝试fiona python 模块(它依赖于 OGR,但提供了一个更简单的 API,尤其是当您只使用属性时)。根据您是否着急完成任务,您还可以使用threading 模块。

标签: python gdal shapefile arcpy ogr


【解决方案1】:

在 Esri ArcGIS 领域中,Update Cursors 通常用于此类操作。

例如

import arcpy

# Your input feature class
fc = r'C:\path\to\your.gdb\feature_class'

# Start an update cursor and change values from 0 to 100 in a field called "your_field"
with arcpy.da.UpdateCursor(fc, "your_field") as cursor:
    for row in cursor:
        if row[0] == 0:
            row[0] = 100
        cursor.updateRow(row)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 2013-02-27
    • 2014-07-12
    • 1970-01-01
    • 2022-11-10
    相关资源
    最近更新 更多