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:
When I call the function:
I get the following error:
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.
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!
Solved! Go to Solution.
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.
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}!)")
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.
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}!)")
Thank you very much for helping with this!
Thank you so much for this! Took care of my issue!