import arcpy
# 设置输入图层路径
polygon_layer = r"PolygonZY" # PolygonZY图层路径
# 使用 UpdateCursor 更新符合条件的要素
with arcpy.da.UpdateCursor(polygon_layer, ["QSLX", "JYFS", "OID@"]) as cursor:
for row in cursor:
# 检查是否符合 QSLX = 2 和 JYFS = 3 条件
if row[0] == 2 and row[1] == 3:
# 这里可以根据需要修改字段值
row[0] = 2 # 将 QSLX 字段设置为 2
row[1] = 3 # 将 JYFS 字段设置为 3
cursor.updateRow(row) # 更新当前行
print("更新完成!")
import arcpy
# 定义源表和目标表
source_table = r"地登记数据1_ExcelToTable" # 源表路径
target_table = r"Homestead" # 目标表路径
# 使用 SearchCursor 遍历源表中的记录
with arcpy.da.SearchCursor(source_table, ['BDCDYH', 'QLR', 'ZL']) as search_cursor:
for search_row in search_cursor:
print(f"源表 BDCDYH: {search_row[0]}, QLR: {search_row[1]}, ZL: {search_row[2]}")
# 使用 UpdateCursor 更新目标表中的记录
with arcpy.da.UpdateCursor(target_table, ['BDCDYH', 'QLRMC', 'TDZL', 'ZDBH', 'QSLXM']) as update_cursor:
for update_row in update_cursor:
if update_row[0] == search_row[0]: # 如果 BDCDYH 匹配
print(f"目标表 BDCDYH: {update_row[0]} 匹配成功")
# 赋值给目标表的相应字段
update_row[1] = search_row[1] # QLR -> QLRMC
update_row[2] = search_row[2] # ZL -> TDZL
# 对 ZDBH 进行操作:取最后部分
zdbh_value = update_row[3] # ZDBH
update_row[3] = zdbh_value[-5:] # 取最后五位值,如 "01003"
# 设置 QSLXM 字段为 "JC"
update_row[4] = "JC"
# 更新目标表中的记录
update_cursor.updateRow(update_row)
#print(f"更新目标表: BDCDYH = {update_row[0]}, QLRMC = {update_row[1]}")
print("数据已成功更新。")
代码解释:
- 输入图层:
polygon_layer
是PolygonZY
图层的路径。 - UpdateCursor:遍历
PolygonZY
图层中的每一行,检查QSLX
和JYFS
字段的值。 - 条件判断:如果
QSLX
等于 2 且JYFS
等于 3,那么就继续进行字段更新。由于我们已经假设这两个字段需要赋值为这两个值,你可以根据实际需要进行进一步修改。 - 更新字段:通过
cursor.updateRow(row)
更新字段值。
扩展:
如果你有其他需要为这两个字段赋不同值的条件,或在更新时使用其他字段的数据,也可以在条件判断后进行相应的修改。