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

1 投票
1 回答
2178 浏览
提问于 2025-04-18 08:40

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

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

撰写回答