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

时间:2014-06-05 04:44:27

标签: python arcgis arcpy arcmap

我正在学习如何使用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(??)

1 个答案:

答案 0 :(得分: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