1

I'm currently trying to update the fields of a shapefile, with a list I've compiled outside of the arcpy environment. Trying both UpdateCursor which I can't seem to get to work, and FieldCalculator. FieldCalculator just updates it with "P[2]"

photoname = []

for p in shp_list:
    name = p[2]
    photoname.append(name)

    # arcpy.CalculateField_management(out_shapefile,"PhotoName","'p[2]'","PYTHON_9.3")

I've also tried:

updt_cursor = arcpy.da.UpdateCursor(out_shapefile, fields)
for row in updt_cursor:
    val = str(row[0])
    for p in shp_list:
        if val in p:
            row[2] = p_name
            updt_cursor.updateRow(row)

And UpdateCursor is blank.

I know I could create a .txt from the list, then update the fields with the line in the .txt file with InsertCursor, but I feel like there should be a more efficient way to do this. For context, I'm creating point shapefiles of the X/Y from the exif data, and want to fill in the corresponding image name.

4
  • 1
    Make sure to close the update cursor. You can do so with del updt_cusor at the end, but the better practice is to use a with block so it gets done automatically. See the examples: pro.arcgis.com/en/pro-app/arcpy/data-access/…
    – mikewatt
    Oct 13, 2020 at 19:58
  • @mikewatt Updated to be consistent with the documentation still no change. Oct 13, 2020 at 20:07
  • 2
    Then you're left with logical errors. The if val in p: condition is never met-- if it was, you would get an exception because you never defined p_name. Do some standard debugging here: print out shp_list to make sure it contains what you expect, print val from within the main loop, print p from within the inner loop, etc.
    – mikewatt
    Oct 13, 2020 at 21:43
  • 1
    Agreed with @mikewatt in that you need to check your iterables. Can you show us what shp_list and some of the corresponding val values?
    – Aaron
    Oct 13, 2020 at 21:51

1 Answer 1

2

To fix your field calculator statement change it to:

arcpy.CalculateField_management(out_shapefile,"PhotoName","'" + p[2] + "'","PYTHON_9.3")
1
  • This works, kind of. now it's just an iteration fix. They're all photo 22. I can figure it out from here. I can't believe I missed something so simple. Oct 14, 2020 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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