ArcPy:如果列A = x中的值,则在值B中创建新表选择列

问题描述 投票:0回答:1

我是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)
python field layer arcpy
1个回答
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问题要好得多

© www.soinside.com 2019 - 2024. All rights reserved.