简体   繁体   English

如何消除 UpdateCursor 上的 elif 语法错误?

[英]How do I get rid of the elif syntax error on my UpdateCursor?

Why am I getting a Syntax error on elif when trying to calculate an index with the UpdateCursor function in Python?为什么在 elif 上尝试使用 Python 中的 UpdateCursor function 计算索引时出现语法错误? This is my code, the second elif always gives a syntax error.这是我的代码,第二个 elif 总是给出语法错误。

    fc = 'Oberbodenproben_mit_Laborwerten_2019_1'
    fields = ['ABAG_K_Faktor','K1','K2','K3','T_proz','U_proz','S_proz','U+ffS_proz','S-ffS_proz','Org_Substanz_proz','Permeabilitätsklasse','Aggregierungsklasse','Steinbedeckung_proz']

Create update cursor for feature class为功能 class 创建更新 cursor

    with arcpy.da.UpdateCursor(fc, fields) as cursor:
        
# For each row, evaluate the WELL_YIELD value (index position 
# of 0), and update WELL_CLASS (index position of 1)

       for row in cursor:   

         if (row[7]<= 70):
           row[1] = 2.77*10**(-5) *((row[7]*(100-row[4]))**1.14
         elif(row[7] > 70):
           row[1] = 1.75*10**(-5)*((row[7]*(100-row[4]))**1.14+0.0024*row[8]+0,161
         elif (row[9] <= 4):
           row[2] = row[1]*(12-row[9])/10
         elif (row[9] > 4):
           row[2] = row[1]*0.8
         elif (row[2] >= 0.2):
           row[3] = row[2]+0.043*(row[11]-2)+0.033*(4-row[10])
         elif (row[2] < 0.2):
           row[3] = 0.091-0.34*row[2]+1.79*row[2]**2+0.24*row[2]*row[11]+0.033*(4-row[10])   
         elif (row[12]<=1,5):
           row[0]=row[3]
         elif (row[12]>1,5):
           row[0]=row[3]*(1.1*math.e**(-0.0024*row[12])-0.06)
               
    # Update the cursor with the updated list
    cursor.updateRow(row)

    return

I already tried getting read of the formulas, but with easy numbers another problem turns up.我已经尝试阅读这些公式,但是对于简单的数字,另一个问题出现了。 Apparently the rows don't exist then.显然这些行当时不存在。

Indents matter for python code.缩进对于 python 代码很重要。 And so do matching ( ) parens.匹配( )括号也是如此。 Here is what you intended:这是您的意图:

    for row in cursor:

        if row[7] <= 70:
            row[1] = 2.77 * 10 ** (-5) * ((row[7] * (100 - row[4])) ** 1.14)
        elif row[7] > 70:
            row[1] = (
                1.75
                * 10 ** (-5)
                * ((row[7] * (100 - row[4])) ** 1.14 + 0.0024 * row[8] + 0, 161)
            )
        elif row[9] <= 4:
            row[2] = row[1] * (12 - row[9]) / 10
        elif row[9] > 4:
            row[2] = row[1] * 0.8
        elif row[2] >= 0.2:
            row[3] = row[2] + 0.043 * (row[11] - 2) + 0.033 * (4 - row[10])
        elif row[2] < 0.2:
            row[3] = (
                0.091
                - 0.34 * row[2]
                + 1.79 * row[2] ** 2
                + 0.24 * row[2] * row[11]
                + 0.033 * (4 - row[10])
            )
        elif (row[12] <= 1, 5):
            row[0] = row[3]
        elif (row[12] > 1, 5):
            row[0] = row[3] * (1.1 * math.e ** (-0.0024 * row[12]) - 0.06)

1.75 * 10 ** (-5) is kind of a curious way to express the constant 1.75e-5 , but I guess it works for you so go for it. 1.75 * 10 ** (-5)是一种表达常量1.75e-5的奇怪方式,但我想它对你有用,所以 go 适合它。


Using the name of each field within a row would be much more readable than indexes like [12] .使用一行中每个字段的名称[12]这样的索引更具可读性。

Consider doing考虑做

from collections import namedtuple`

Foo = namedtuple("Foo", fields)

(It's not yet clear what each row represents -- choose a more descriptive name than Foo.) (目前还不清楚每一行代表什么——选择一个比 Foo 更具描述性的名称。)

https://docs.python.org/3/library/collections.html#collections.namedtuple https://docs.python.org/3/library/collections.html#collections.namedtuple

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM