arcpy给shp的字段逐行修改\添加值

一、给字段修改和添加值用到“arcpy.UpdateCursor”方法,加载shp图层

shppath = r'G:\Desktop\Point.shp'
cursor = arcpy.UpdateCursor(shppath)

二、将准备修改/添加的值放到list中

listc = [1, 5, 8, 9, 13]

三、循环游标,逐行更新新的字段值、此处以shp属性表的“test”字段为例,用到“updateRow”进行值的更新

i = 0
for my_row in cursor:
    my_value = my_row.getValue('test')
    my_row.setValue('test', listc[i])
    cursor.updateRow(my_row)
    i += 1

完整代码如下:

# coding:utf-8
import arcpy

filename = 'G:\Desktop'
arcpy.env.workspace = filename

listc = [1, 5, 8, 9, 13]

shppath = r'G:\Desktop\Point.shp'
cursor = arcpy.UpdateCursor(shppath)

i = 0
for my_row in cursor:
    my_value = my_row.getValue('test')
    my_row.setValue('test', listc[i])
    cursor.updateRow(my_row)
    i += 1

转载自:https://blog.csdn.net/qq_31967985/article/details/80203060

You may also like...