Problem creating function containing CalculateField

433
4
Jump to solution
02-10-2024 07:59 PM
TimPennington
New Contributor II

I am very new to both arcgis and python. I am trying to define a function that takes in an input shape file, 2 fields from the file to perform a calculation on, a new field in the file to store the calculation, and an output shapefile name.

I have set up the following in Jupyter:

TimPennington_0-1707623280725.png

When I call the function:

TimPennington_3-1707623519615.png

I get the following error:

TimPennington_1-1707623374196.pngTimPennington_2-1707623387902.png

If I add lines to the function to print the inputs, and comment out CalculateField I get :

!Height!

!Area!

!Volume!

 

I have also tried just passing field 1 and field 2 into CalculateField and I get the same error.

If I run CalculateField without using the function, it runs with expected results.

TimPennington_4-1707623589169.png

I am not sure what I need to do to pass the parameters of the function through CalculateField.

Any guidance available would be greatly appreciated. Thank You!

 

 

 

0 Kudos
2 Solutions

Accepted Solutions
AlfredBaldenweck
MVP Regular Contributor

Try using an f-string:

 

arcpy.management.CalculateField(dataset_out, input3, f"int({input1})* int({input2})")

 

The f-string will sub in a variable into the string, just like using "int({})* int({})".format(input1, input2) 

(I think that's the syntax for the old way? I don't use it anymore.)  Okay, that was actually a correct way lol.

View solution in original post

BlakeTerhune
MVP Regular Contributor

I think @AlfredBaldenweck is correct here. You could also format your field names with the exclamations in this f-string instead of as separate variables. Also, the second parameter (field) doesn't need the exclamations.

arcpy.management.CalculateField(dataset_out, field3, f"int(!{field1}!) * int(!{field2}!)")

 

View solution in original post

4 Replies
AlfredBaldenweck
MVP Regular Contributor

Try using an f-string:

 

arcpy.management.CalculateField(dataset_out, input3, f"int({input1})* int({input2})")

 

The f-string will sub in a variable into the string, just like using "int({})* int({})".format(input1, input2) 

(I think that's the syntax for the old way? I don't use it anymore.)  Okay, that was actually a correct way lol.

BlakeTerhune
MVP Regular Contributor

I think @AlfredBaldenweck is correct here. You could also format your field names with the exclamations in this f-string instead of as separate variables. Also, the second parameter (field) doesn't need the exclamations.

arcpy.management.CalculateField(dataset_out, field3, f"int(!{field1}!) * int(!{field2}!)")

 

TimPennington
New Contributor II

Thank you very much for helping with this!

0 Kudos
TimPennington
New Contributor II

Thank you so much for this! Took care of my issue!

0 Kudos