使用python中的几个条件从表中获取特定值
问题描述:
我有一个表,表中的每个不同值都是不同条件组合的结果。例如,在下图中,条件如下:如果coverType =休闲,Treatment = Crop Residue Cover,Condition/ImperviousArea = Good,SoilType = C,那么该值等于83.我想要一个工具,要求用户从每列中选择一个值(例如,选择CoverType; SoilType,...),然后返回相关的数字作为输出。你有什么想法我应该怎么做? 到目前为止,我有,就像下面的代码的第一行: 使用python中的几个条件从表中获取特定值
import arcpy
path= r'H\Python\FinalProject\RunOff.gdb'
arcpy.env.workspace= path
arcpy.env.overwriteOutput = True
table=r'H\Python\FinalProject\RunOff.gdb\CN.csv'
cursor= arcpy.SearchCursor(table)
答
除非你前面限制到ArcGIS 10.0,我建议使用da cursors。语法更容易一些。
但是,你绝对是在正确的轨道上。通过光标,您可以循环查看表格,并查找三个类别与用户输入相匹配的行。
cover_type = #userinput
treatment = #userinput
condition = #userinput
field_list = ["CoverType", "Treatment", "Condition", "B"]
with arcpy.da.SearchCursor(table, field_list) as cursor:
for row in cursor:
if row[0] == cover_type and row[1] == treatment and row[2] == condition:
print row[3]
else:
print "no matching combination"
如果它的ArcPy我认为你在[gis.se] Stack Exchange上更好地研究/询问。 – PolyGeo