-1

Attempting to make the following code work in ArcMap:

for i in range(len(l)):
print("calculating area" + str(i))
in_layer = l[i]
exp = "!SHAPE.AREA@ACRES!"
arcpy.CalculateField_management(in_layer, "Area_Field", exp, "PYTHON 9.3")
print("next layer" + str(i+1))

Getting the following error:

enter image description here

Trying to follow the first answer in the link here Calculate area within Python script in ArcMap, for reference.

Is the expression wrong, or is there a different error in the code?

I am using ArcMap 10.5.1.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
arc_user
  • 59
  • 8

1 Answers1

0

The code below worked:

for i in range(len(l)):
    print("calculating area" + str(i))
    in_layer = l[i]
    arcpy.CalculateField_management(in_layer, "Area_Field", "!shape.area@acres!", "PYTHON_9.3")
    print("next layer" + str(i + 1))

"Area_Field" was the new field that was created previously and that I was trying to fill out in each layer. Not sure what the error was caused by before.

Following this tutorial also helped: https://www.youtube.com/watch?v=BKp7O6EWCLA

arc_user
  • 59
  • 8
  • Where is l set? If I were to run either this code or the code you presented in your question I would get an error immediately. – PolyGeo Jan 15 '23 at 07:24
  • 1
    The reason for the original error, which you corrected in this code, was the 'PTYHON 9.3'. You had a space in between PYTHON and 9.3, which was incorrect. – bixb0012 Jan 15 '23 at 15:38
  • 1
    I suggest you do a search and read up on loops in Python. The code structure being used for looping may work, but it is the least Pythonic way to do it. – bixb0012 Jan 15 '23 at 15:39
  • Ah, that is true, I did miss the space. And thank you for the suggestion, I will look into it! Am in the process of going through a Python course, but not advanced enough yet to make the loops more compact. Was planning on rewriting the code after I study some more. Will study up more on loops and cursors. – arc_user Jan 15 '23 at 19:17
  • I will keep in mind for making the error messages as formatted text for next time. – arc_user Jan 15 '23 at 19:17
  • I did not include setting the l in this portion, because usually I just put "l=" in the python window, and drag the list of layers that I need – arc_user Jan 15 '23 at 19:18