1

我正在学习如何使用 Python 创建在 ArcMap (10.1) 中运行的脚本。下面的代码让用户选择 shapefile 所在的文件夹,然后遍历 shapefile 以创建仅包含以“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(??)
4

1 回答 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
于 2014-06-05T18:11:23.943 回答