Select to view content in your preferred language

how to update field with name of FC for list of FCs ?

5768
16
Jump to solution
02-10-2015 02:49 PM
GabrielBacca-Cortes
Deactivated User

Hi Everybody -

I am trying to fill in the attribute in a newly created field 'Particle', with the name of the FC, which will be changing for a list of FCs. I've tried using 'CalculateField', with which I get the error: 'NameError: name 'xy_1' is not defined', where 'xy_1' is the name of the FC going through the loop.

I've tried using 'UpdateCursor' and I get the error: "  'list' object has no attribute 'setValue'  ". 

This is the code I've written so far:

fieldName1 = "Particle"

fieldType1 = "TEXT"

fieldlength = 20

wildcard = ""

fctype = ""

try:

    fcList =  arcpy.ListFeatureClasses(wildcard, fctype)

    # print fcList2

    for fc in fcList:

        print fc

        arcpy.AddField_management(fc,fieldName1,fieldType1,"","",fieldlength,"","NULLABLE")

[with:]

        desc = arcpy.Describe(fc)

        val = desc.name

        print val

        arcpy.CalculateField_management(fc, fieldName1, val, "PYTHON_9.3")

[OR:]

##        curs =  arcpy.da.UpdateCursor(fc,fieldName1)

##        for row in curs:

##            desc = arcpy.Describe(fc)

##            val = desc.name

##            print val

##            row.setValue(fieldName1,val)

##        curs.updateRow(row)

##        del curs, row

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

You could do something like this (if you have ArcGIS 10.1 SP1 or higher):

import arcpy, os

# settings for field
fld_name = "Particle"
fld_type = "TEXT"
fld_length = 20

# set you workspace (where the fc's are located)
ws = r"C:\Path\To\Your\Folder\or\fgdb.gdb"
arcpy.env.workspace = ws

fcs =  arcpy.ListFeatureClasses()
for fc_name in fcs:
    fc = os.path.join(ws, fc_name)
    # add field, if it doesn't exist
    if len(arcpy.ListFields(fc, wilc_card=fld_name)) == 0:
        arcpy.AddField_management(fc, fld_name, fld_type, field_length=fld_length)

    with arcpy.da.UpdateCursor(fc, (fld_name)) as curs:
        for row in curs:
            row[0] = fc_name
            curs.updateRow(row)
    del curs, row

View solution in original post

16 Replies
JoshuaBixby
MVP Esteemed Contributor

Review the documentation on ArcPy Data Access cursors.  The arcpy.da update cursor doesn't have a setValue method, that was for the older-style update cursor.  You are effectively mixing up the syntax for the two types of cursors.

0 Kudos
GabrielBacca-Cortes
Deactivated User

Thanks Joshua -  Yes, you're right I was mixing up 'da' cursors with the older version.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

With CalculateField_management, try removing "PYTHON_9.3".  You aren't actually passing an expression, just a value. 

0 Kudos
TomSellsted
MVP Alum

Gabriel,

It look like you may be confusing how you are using cursors in python.  The Data Access module cursors have some distinct differences.  You don't need to specify the field for your cursor.  This might work:

        curs =  arcpy.UpdateCursor(fc)

        for row in curs:

            desc = arcpy.Describe(fc)

            val = desc.name

            print val

            row.setValue(fieldName1,val)

            curs.updateRow(row)

        del curs, row

Notice that I have removed the .da. from your cursor reference and indented the updateRow line.

Regards,

Tom

0 Kudos
GabrielBacca-Cortes
Deactivated User

Hi Tom -  thanks for the response.  Yes, I was mixing 'DA' cursors with the older version.

the changes worked.

regards,

Gabriel

0 Kudos
TomSellsted
MVP Alum

You are welcome Gabriel!

0 Kudos
XanderBakker
Esri Esteemed Contributor

You could do something like this (if you have ArcGIS 10.1 SP1 or higher):

import arcpy, os

# settings for field
fld_name = "Particle"
fld_type = "TEXT"
fld_length = 20

# set you workspace (where the fc's are located)
ws = r"C:\Path\To\Your\Folder\or\fgdb.gdb"
arcpy.env.workspace = ws

fcs =  arcpy.ListFeatureClasses()
for fc_name in fcs:
    fc = os.path.join(ws, fc_name)
    # add field, if it doesn't exist
    if len(arcpy.ListFields(fc, wilc_card=fld_name)) == 0:
        arcpy.AddField_management(fc, fld_name, fld_type, field_length=fld_length)

    with arcpy.da.UpdateCursor(fc, (fld_name)) as curs:
        for row in curs:
            row[0] = fc_name
            curs.updateRow(row)
    del curs, row
GabrielBacca-Cortes
Deactivated User

thanks Xander - this works too, for a more general case.  Thanks again..

0 Kudos
GabrielBacca-Cortes
Deactivated User

Xander -  this general version works great when one has to re-run the script through the same set of FCs without redoing previous work. I am sure you knew this but I just figured it out, 🙂  Thanks a bunch !

0 Kudos