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