2

I want assign 33 if the field no_apt is zero and 34 if no_apt is any else value. The model run without any error, but no value are assigned the field GGV_kW. What is wrong with the code since it doesn't work?

The code used is:

in_Table=nodesFeatureoutall
fieldName="GGV_kW"
expression="GGV_calculation( !no_apt!, !GGV_kW!)"

codeblock = """

def GGV_calculation(no_apt,GGV_kW):

    if no_apt==0:
        return 33
    else:
        return 34"""

arcpy.CalculateField_management(in_Table,fieldName,expression,"PYTHON",codeblock)
3

2 Answers 2

1

As an alternative to Field Calculator you can use the da.UpdateCursor :

import arcpy

in_Table = r"C:\somedatabase.gdb\nodesFeatureoutall"
fieldNames = ["GGV_kW","no_apt"]

with arcpy.da.UpdateCursor(in_Table,fieldNames) as cursor:
    for row in cursor:
        if row[1]==0: #index one in fieldNames list is the second element (no_apt)
            row[0]=33 #index zero in fieldNames list is the first element (GGV_kW)
        else:
            row[0]=34
        cursor.updateRow(row)
0

I have run your code exactly as it is and it works fine here. My only changes to your code was that I added the following two lines at the beginning:

import arcpy

nodesFeatureoutall = r"C:\...\Default.gdb\nodesFeatureoutall"

I have tried it with no_apt as a short integer field where it works as expected, and as a text field, where it always gets a 34 in the 'GGV_kW' field.

So the short answer to your question is that your code DOES work.

How did you determine that the code does not work?

Are you looking at the results in the attribute table in ArcMap? This table does not refresh automatically. You need to close it and re-open it (or use one of the other techniques for updating the data source of the displayed open attribute table).

I think you need to provide more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.