如何在Python中向值表(ArcPy)添加行?

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

我正在学习如何使用Python创建在ArcMap(10.1)中运行的脚本。下面的代码让用户选择shapefile所在的文件夹,然后通过shapefile创建一个Value Table,其中只包含那些以“landuse”开头的shapefile。

我不确定如何在值表中添加行,因为在参数中选择了值,并且无法将文件夹直接放入代码中。见下面的代码......

#imports
import sys, os, arcpy 

#arguments
arcpy.env.workspace = sys.argv[1] #workspace where shapefiles are located

#populate a list of feature classes that are in the workspace
fcs = arcpy.ListFeatureClasses()

#create an ArcGIS desktop ValueTable to hold names of all input shapefiles
#one column to hold feature names
vtab = arcpy.ValueTable(1)

#create for loop to check for each feature class in feature class list
for fc in fcs:
    #check the first 7 characters of feature class name == landuse
    first7 = str(fc[:7])
    if first7 == "landuse":
        vtab.addRow() #****THIS LINE**** vtab.addRow(??)
python arcgis arcpy arcmap
1个回答
2
投票

for循环中,fc将作为fcs列表中每个要素类的字符串。因此,当您使用addRow方法时,您将传递fc作为参数。

这是一个可能有助于澄清的例子:

# generic feature class list
feature_classes = ['landuse_a', 'landuse_b', 'misc_fc']

# create a value table
value_table = arcpy.ValueTable(1)

for feature in feature_classes:       # iterate over feature class list
    if feature.startswith('landuse'): # if feature starts with 'landuse'
        value_table.addRow(feature)   # add it to the value table as a row

print(value_table)

>>> landuse_a;landuse_b
© www.soinside.com 2019 - 2024. All rights reserved.