使用arcpy按属性选择并删除所选记录。

huangapple go评论70阅读模式
英文:

Select by attributes and delete selected records with arcpy

问题

从Excel文件中导出了11个表格到文件地理数据库(gdb)中。

这些表格具有相同的结构(相同字段),但包含不同的数据。

表格中的记录包含无用数据,需要在脚本处理中删除。

在ArcGIS Pro中,我使用"Select by Attributes"工具删除无用数据:

选择 [Where] [FieldName] [包含文本] [uselesstext]

然后删除所选记录。

但我需要将这个过程放入脚本中,尽管找不到创建该功能的适当代码或提示。

更新后的代码:

import arcpy
import re
fc = r'D:\YourFolderName\YourGDBName.gdb\YourFeatureClassName'

with arcpy.da.UpdateCursor(fc, "YourFieldName") as cursor:
    for row in cursor:
        if re.match("YourWordHere.+", row[0]):
            cursor.deleteRow()

此代码在字段开头匹配搜索词时有效。

import arcpy
import re
fc = r'D:\YourFolderName\YourGDBName.gdb\YourFeatureClassName'

with arcpy.da.UpdateCursor(fc, "YourFieldName", where_clause="YourFieldName LIKE '%YourWordHere%'") as cursor:
    for row in cursor:
        cursor.deleteRow()

此代码在字段的任何位置匹配搜索词时有效。

英文:

Got 11 Tables exported to gdb from excel files.

Those Tables have the same structure (same fields) but different data.

Records from the Tables contain useless data which needs to be deleted in the script process.

In ArcGIS Pro I delete that useless data with the Select by Attributes tool:

Select [Where] [FieldName] [contains the text] [uselesstext]

And then delete the selected records.

But I need to put this process into the script though can not find the proper code or hint to create the function.

Updated code:

import arcpy
import re
fc = r'D:\YourFolderName\YourGDBName.gdb\YourFeatureClassName'

with arcpy.da.UpdateCursor(fc, "YourFieldName") as cursor:
    for row in cursor:
        if re.match(r"YourWordHere.+", row[0]):
            cursor.deleteRow()

This code works when searched word stands at start of the field.

import arcpy
import re
fc = r'D:\YourFolderName\YourGDBName.gdb\YourFeatureClassName

with arcpy.da.UpdateCursor(fc, "YourFieldName", where_clause="YourFieldName LIKE '%YourWordHere%'") as cursor:
    for row in cursor:
        cursor.deleteRow()

This code works with a word at any part of the field.

答案1

得分: 0

if row[0] is LIKE regist% 不是有效的Python代码,你不能直接使用SQL语法这样写。相反,尝试使用Python语法重新表达条件。例如:

import re

with arcpy.da.UpdateCursor(fc, "owner") as cursor:
    for row in cursor:
        if re.match(r"regist.+", row[0]):
            cursor.deleteRow()

另一个选项是将类似SQL的条件放在UpdateCursor本身中:

with arcpy.da.UpdateCursor(fc, "owner", where_clause="owner LIKE 'regist%'") as cursor:
    for row in cursor:
        cursor.deleteRow()
英文:

if row[0] is LIKE regist% is not a valid python code, you can't use SQL syntax directly like this. Instead, try to rephrase the condition with python syntax. For example:

import re

with arcpy.da.UpdateCursor(fc, "owner") as cursor:
    for row in cursor:
        if re.match(r"regist.+", row[0]):
            cursor.deleteRow()

Another option is to put the SQL-like condition in the UpdateCursor itself:

with arcpy.da.UpdateCursor(fc, "owner", where_clause="owner LIKE 'regist%'") as cursor:
    for row in cursor:
        cursor.deleteRow()

huangapple
  • 本文由 发表于 2023年2月9日 03:41:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定