Create a copy of an existing attribute table field

840
6
Jump to solution
08-12-2013 08:18 AM
BrendanWhite1
New Contributor
What I am attempting to do is create a copy of an attribute field in a feature class. I want the values of both fields to be exactly the same, but I want their titles to be different. I think I'm close but I am receiving errors, and cannot resolve them myself.

import arcpy, string, os, fileinput
from arcpy import env
env.workspace = "filepath/filepath.gdb"
env.overwriteOutput = True
fc = "FeatureClass"
verification = arcpy.Exists (fc)
print verification
del verification

arcpy.AddField_management (fc, "NewField", "TEXT", "", "", "", "", "", "", "")
cursor = arcpy.da.UpdateCursor (fc, "NewField")
for row in cursor:
   delimfield = arcpy.AddFieldDelimiters (fc, ["OldField"])
   row[0]
   cursor.UpdateRow(row)
del row
del cursor


I think my issue is here: "delimfield = arcpy.AddFieldDelimiters (fc, ["OldField"])" I'm not even sure this is the right tool for copying over the values from one field to the other. After running the script I receive the error listed below. Any ideas?

Runtime error 
Traceback (most recent call last):
  File "<string>", line 22, in <module>
AttributeError: 'da.UpdateCursor' object has no attribute 'UpdateRow'
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
T__WayneWhitley
Frequent Contributor
So, something like this, but you have to test it yourself:
import arcpy from arcpy import env  env.overwriteOutput = True env.workspace = "filepath/filepath.gdb"  fc = "FeatureClass" fields = ('OldField', 'NewField')  #Add the new field arcpy.AddField_management (fc, "NewField", "TEXT")  # Create update cursor for feature class  # with arcpy.da.UpdateCursor(fc, fields) as cursor:     # For each row, read the OldField value (index position      #  of 0), and update NewField value (index position of 1)     #     for row in cursor:         row[1] = row[0]          # Update the cursor with the updated list         #         cursor.updateRow(row)



PS - Incidentally, you may just have a case-sensitivity error in:
cursor.UpdateRow(row)

...since 'UpdateRow' is not recognized, you may fix your own error with 'updateRow'.
I just noticed that.

View solution in original post

0 Kudos
6 Replies
AngelaHansen
New Contributor
You should be able to do something like this:

for row in cursor:
    row.setValue("NewField", row.getValue("OldField"))
    cursor.updateRow(row)


What I am attempting to do is create a copy of an attribute field in a feature class. I want the values of both fields to be exactly the same, but I want their titles to be different. I think I'm close but I am receiving errors, and cannot resolve them myself.  

import arcpy, string, os, fileinput
from arcpy import env
env.workspace = "filepath/filepath.gdb"
env.overwriteOutput = True
fc = "FeatureClass"
verification = arcpy.Exists (fc)
print verification
del verification

arcpy.AddField_management (fc, "NewField", "TEXT", "", "", "", "", "", "", "")
cursor = arcpy.da.UpdateCursor (fc, "NewField")
for row in cursor:
   delimfield = arcpy.AddFieldDelimiters (fc, ["OldField"])
   row[0]
   cursor.UpdateRow(row)
del row
del cursor


I think my issue is here: "delimfield = arcpy.AddFieldDelimiters (fc, ["OldField"])" I'm not even sure this is the right tool for copying over the values from one field to the other. After running the script I receive the error listed below. Any ideas?  

Runtime error 
Traceback (most recent call last):
  File "<string>", line 22, in <module>
AttributeError: 'da.UpdateCursor' object has no attribute 'UpdateRow'
0 Kudos
T__WayneWhitley
Frequent Contributor
The error states UpdateRow is invalid...it's because that's a method on a row object -- so I suspect you are trying to use this syntax instead which establishes a loop on the rows returned by the cursor:

with arcpy.da.UpdateCursor(fc, fields) as cursor


See the samples listed at the webhelp here:

UpdateCursor (arcpy.da)
Desktop » Geoprocessing » ArcPy » Data Access module
http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000014000000


Looks like you need to establish opening a cursor, specifying 2 fields representing the 'old' and the 'new' (see example 1), then loop to capture the val from the old to write that in the new field, correct?

Hope that helps,
Wayne
0 Kudos
T__WayneWhitley
Frequent Contributor
So, something like this, but you have to test it yourself:
import arcpy from arcpy import env  env.overwriteOutput = True env.workspace = "filepath/filepath.gdb"  fc = "FeatureClass" fields = ('OldField', 'NewField')  #Add the new field arcpy.AddField_management (fc, "NewField", "TEXT")  # Create update cursor for feature class  # with arcpy.da.UpdateCursor(fc, fields) as cursor:     # For each row, read the OldField value (index position      #  of 0), and update NewField value (index position of 1)     #     for row in cursor:         row[1] = row[0]          # Update the cursor with the updated list         #         cursor.updateRow(row)



PS - Incidentally, you may just have a case-sensitivity error in:
cursor.UpdateRow(row)

...since 'UpdateRow' is not recognized, you may fix your own error with 'updateRow'.
I just noticed that.
0 Kudos
BrendanWhite1
New Contributor
Taking Wayne's advice into consideration, I have changed the cursor as shown below.  However I am not getting any error, but the "NewField" is not updating to reflect the "OldField" values.  They are still null.  This says to me the script is just skipping the cursor.  Any suggestions?

import arcpy, string, os, fileinput
from arcpy import env
env.workspace = "filepath/file.gdb"
env.overwriteOutput = True
fc = "feature class"
verification = arcpy.Exists (fc)
print verification
del verification


arcpy.AddField_management (fc, "NewField", "TEXT", "", "", "", "", "", "", "")
fields  = ("OldField", "NewField")
with arcpy.da.UpdateCursor (fc, fields) as cursor:
   for row in cursor:
      row[1] = row[0]
      cursor.updateRow(row)
del row
del cursor
0 Kudos
RhettZufelt
MVP Frequent Contributor
Working just fine for me as long as OldField actually exists in the FC.

Could change to this for help trouble shooting:

with arcpy.da.UpdateCursor(fc, fields) as cursor:
   for row in cursor:
      row[1] = "test"
      cursor.updateRow(row)


If this works, and codes all values in NewField to "test", then it is just not getting the correct input. Are you sure OldField is not nulls here as I get exactly the same in my new column.

R_
0 Kudos
BrendanWhite1
New Contributor
So, something like this, but you have to test it yourself:
import arcpy
from arcpy import env

env.overwriteOutput = True
env.workspace = "filepath/filepath.gdb"

fc = "FeatureClass"
fields = ('OldField', 'NewField')

#Add the new field
arcpy.AddField_management (fc, "NewField", "TEXT")

# Create update cursor for feature class 
#
with arcpy.da.UpdateCursor(fc, fields) as cursor:
    # For each row, read the OldField value (index position 
    #  of 0), and update NewField value (index position of 1)
    #
    for row in cursor:
        row[1] = row[0]

        # Update the cursor with the updated list
        #
        cursor.updateRow(row)



PS - Incidentally, you may just have a case-sensitivity error in:
cursor.UpdateRow(row)

...since 'UpdateRow' is not recognized, you may fix your own error with 'updateRow'.
I just noticed that.



My problem was the capitalization, Wayne.  Thank you for pointing that out.  Being new to Python (programming in general) capitalization kills me.  Thank you everyone for the help.
0 Kudos