gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:11:50 29 4
gpt4 key购买 nike

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

关于python - 如何在 Python 中向值表 (ArcPy) 添加行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24051793/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com