如何用ArcPy实现按属性筛选并删除GDB中多表的无用记录?
批量删除GDB表格中含指定文本的记录
- 已将11个同结构(字段一致)的Excel表格导入GDB,需批量删除表格中包含指定无用文本的记录
- 原手动操作:在ArcGIS Pro中使用「按属性选择」工具,设置筛选条件为
[FieldName] contains the text [uselesstext],随后删除选中记录
场景1:删除字段以指定文本开头的记录
通过正则匹配字段开头的目标文本,匹配成功则删除对应行:
import arcpy import re # 替换为你的要素类路径 fc = r'D:\YourFolderName\YourGDBName.gdb\YourFeatureClassName' with arcpy.da.UpdateCursor(fc, "YourFieldName") as cursor: for row in cursor: # 匹配以YourWordHere开头的文本,.+表示后续可接任意字符 if re.match(r"YourWordHere.+", row[0]): cursor.deleteRow()
场景2:删除字段任意位置包含指定文本的记录
直接通过UpdateCursor的where_clause筛选符合条件的记录,效率更高:
import arcpy # 替换为你的要素类路径 fc = r'D:\YourFolderName\YourGDBName.gdb\YourFeatureClassName' # LIKE '%YourWordHere%' 匹配字段内任意位置包含指定文本的记录 with arcpy.da.UpdateCursor(fc, "YourFieldName", where_clause="YourFieldName LIKE '%YourWordHere%'") as cursor: for row in cursor: cursor.deleteRow()
批量处理11个表格的优化方案
如果要一次性处理GDB内的所有目标表格,可遍历GDB中的表格批量执行:
import arcpy import os # 替换为你的GDB路径 gdb_path = r'D:\YourFolderName\YourGDBName.gdb' target_field = "YourFieldName" useless_text = "YourWordHere" # 设置ArcGIS工作空间为目标GDB arcpy.env.workspace = gdb_path # 获取GDB内的所有表格(可根据名称前缀等规则过滤) tables = arcpy.ListTables() for table in tables: print(f"正在处理表格:{table}") with arcpy.da.UpdateCursor(table, target_field, where_clause=f"{target_field} LIKE '%{useless_text}%'") as cursor: for row in cursor: cursor.deleteRow() print(f"表格 {table} 处理完成")
内容的提问来源于stack exchange,提问作者ZaZa M.
相关产品推荐
相关产品推荐

