我是arcpy的新手,正在尝试从现有表创建新表。我要创建表:如果type_1
等于H
,则导出comment_1
中包含的值。
输入图层/属性表:
Number type_1 value_1 comment_1 type_2 value_1 comment_2
23587 H abcdef xyz something
13878 P sdferw H jldoeroslj
156798 Y eiroeow H dfadfsdf
输出表:
Number comment_1 comment_2
23587 abcdef
13878 jldoeroslj
156798 dfadfsdf
我尝试了以下操作,但输出不是我想要的东西:
import arcpy
keepFieldList = ('Number', 'comment_1','comment_2')
Trees = "layername"
fieldInfo=""
fieldList = arcpy.ListFields(layername)
for field in fieldList:
if field.name not in keepFieldList:
fieldInfo = fieldInfo + field.name + " " + field.name + " HIDDEN;"
arcpy.MakeFeatureLayer_management("layername", "outputlayer", "", "",
fieldInfo)
答案 0 :(得分:0)
您可以复制shapefile,然后循环浏览该复制文件中的行,删除不符合您条件的行:
import arcpy
input_shp = r'C:\Users\path\whatever.shp'
copy_shp = r'C:\Users\path\myfiltered.shp'
arcpy.CopyFeatures_management(input_shp, copy_shp)
with arcpy.da.UpdateCursor(copy_shp, 'type_1') as cursor:
for row in cursor:
if row[0] != 'H':
cursor.deleteRow()
或另一种方法,如果必须对许多字段和字符串组合执行此操作,则使用SQL表达式,删除复制文件中不符合您条件的行:
import arcpy
input_shp = r'C:\Users\path\whatever.shp'
copy_shp = r'C:\Users\path\myfiltered.shp'
fieldname = 'type_1'
keepValue = 'H'
sql = fieldname + " <> " + "'" + str(keepValue) + "'"
with arcpy.da.UpdateCursor(copy_shp, fieldname, sql) as cursor:
for row in cursor:
cursor.deleteRow()
最后,描述了第三种方式here
但是正如@PolyGeo所说,GIS堆栈交换论坛对于GIS问题要好得多