0

Here's a stab at my first practical ArcPy script.

I want to grab a layer by name (rather than by layer order or by file path) in the current MXD, then loop over it's attributes (with a view to modifying them). However I don't know the syntax of connecting the desired layer to the attribute table

Here's what I've got so far:

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
needle = "1km_Grid"
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
 if lyr.name == needle:
 # curObj = this layer # pseudo code!
columns = ["MID", "Shape"]
for row in curObj:
 print ("column 1 is: %s" % (row[0]))
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jul 11, 2016 at 12:38

1 Answer 1

2

After you get the lyr.name, use the Update Cursor to loop over the attributes and make changes as needed.

if lyr.name == needle:
 with arcpy.da.UpdateCursor(lyr, columns) as cursor:
 for row in cursor:
 print "column 1 is: {}".format(row[0])
answered Jul 11, 2016 at 12:42
3
  • For the win how do I then update the attribute table? arcpy.Buffer_analysis(columns) doesn't work. Commented Jul 11, 2016 at 13:14
  • The buffer method buffers features not table attributes. If you want to run the buffer method on the layer you do not need to use the cursor. If you need to run the buffer on only certain features within the layer you will need to convert the layer to a feature layer and then make a selection on the layer/features then use the buffer method. Note, that buffer method creates a new layer. Commented Jul 11, 2016 at 13:46
  • Ahh, my confusion I thought you needed to run than after updating a table. Commented Jul 11, 2016 at 14:53

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.