2014-06-05 45 views
1

我正在學習如何使用Python創建在ArcMap(10.1)中運行的腳本。 下面的代碼讓用戶選擇shapefile所在的文件夾,然後通過shapefile創建一個只包含以「landuse」開頭的shapefile的值表。如何將一行添加到Python中的值表(ArcPy)?

我不確定如何向值表中添加一行,因爲值在參數中被選中,並且該文件夾不能直接放入代碼中。請參見下面的代碼...

#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(??) 

回答

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 
相關問題