在python中使用一些条件从表中获取特定值

2024-06-16 11:50:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个表,表中每一个不同的值都是不同值组合的结果条件。用于下图中的示例条件如下:如果coverType=fallow,Treatment=Crop reside Cover,condition/ImperviousArea=Good,SoilType=C,那么值等于83。我想要一个工具,让用户选择每列中的一个值(例如,选择CoverType;SoilType,…),然后返回相关的数字作为输出。你觉得我该怎么做? 到目前为止,我只有以下代码的第一行: enter image description here

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)

Tags: pathcropenv示例table条件finalprojectarcpy
1条回答
网友
1楼 · 发布于 2024-06-16 11:50:28

除非您仅限于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"

相关问题 更多 >