Question

I am learning how to use Python to create scripts that are run in ArcMap (10.1). The code below has the user pick the folder where the shapefiles are located, then goes through the shapefiles to create a Value Table of only those shapefiles that start with "landuse".

I am unsure of how to add a row to the value table, because the values are being selected in an argument, and the folder cannot be put directly into the code. See code below...

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

Solution

In the for loop, fc will be the name as a string of each feature class in the list fcs. So, when you use the addRow method, you'll pass fc as an argument.

Here's an example that might help clarify:

# 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top