3

I am struggling to calculate the acreage as a percentage in Calculate Field Management, but I am getting this error message:

"ValueError: could not convert string to float! BufferAcreage!

arcpy.CalculateField_management(fc, "BuffAcreage", "!shape.area@acres!", "PYTHON", "")

arcpy.CalculateField_management(fc, "BuffPct", float("!BufferAcreage!")/float("!Acres_Whole!") * 100, "PYTHON", "")

The initial line of code works perfectly, but it is the second line of code when I am calculating the acreage percentage that I run into the error.

It will not calculate the field because for some reason it is telling me that I am treating the values in these attributes as strings and will not convert them to floats.

It must be done with the arcpy.CalculateField_management(). I can not use an alternative python scripting method because the assignment directions stipulated that I must use the Calculate Field tool in my script. Any suggestions?

3

The problem stems from the encapsulation of the arguments passed to Calculate Field, from the help:

CalculateField_management (in_table, field, expression, {expression_type}, {code_block})

Your calculation syntax float("!BufferAcreage!")/float("!Acres_Whole!") * 100 is undecipherable by the python parser to the function, as it's several units separated by a space. Each element passed must be a single unit (long, string, list, tuple etc..) which is why the first statement worked fine but the 2nd had a hiccup.

Amending the calculation syntax to:

arcpy.CalculateField_management(fc, "BuffPct", "float(!BufferAcreage!)/float(!Acres_Whole!) * 100", "PYTHON")

parses the calc as a single string which the calculator will substitute !field! with the field value for each row/feature. Note any {parameter} is optional so you don't need ,"") at the end for the code block so I've removed it.

  • Thank you very much! I am still a novice when it comes to Python let alone coding in general, but I hope to continue to learn how to incorporate Python into geoprocessing. – tjwashin234 Mar 27 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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