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)
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Feb 7, 2019 at 19:10
3
  • Check to make sure your field no_apt is a numeric field. Commented Feb 7, 2019 at 23:51
  • What is variable nodesFeatureoutall defined to? Commented Feb 7, 2019 at 23:54
  • gis.stackexchange.com/questions/32522/… Commented Feb 8, 2019 at 6:21

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)
answered Feb 8, 2019 at 6:24
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.

answered Feb 8, 2019 at 4:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.