0

The following script is suppose to do the following, similar to what was done here: Renaming field using ArcPy?.

  1. Create a fieldlist.
  2. Read thru the fieldlist and create a new field named "OLD_" + field.name.
  3. Copy the values in field to new field.

    import arcpy, os
    
    intable = arcpy.GetParameter(0)
    
    fields = arcpy.ListFields(intable)
    
    try:
        for field in fields:
            if field.name == "OBJECTID" or field.name == "Shape" or field.name == "Shape_Length":
                pass
            else:
                field_name = field.aliasName
                old_field = field_name + "OLD_"
                field_precision = field.precision
                field_scale = field.scale
                field_length = field.length
                field_type = field.type
    
                arcpy.AddField_management(intable, old_field, field_type, field_precision, field_scale, field_length)
    
                arcpy.CalculateField_management(intable, old_field, "!"+field+"!")        
    except:
        print arcpy.GetMessages()
    
    del intable, fields, field
    

Two problems:

  1. The script stops upon reaching the CalculateField_management code, which is one iteration, but there are approximately 50 more fields in the list that are ignored.
  2. The newly create field is empty, the values are not copied across.

I have tried everything and followed the example given in the link above, but nothing has fixed these two problems.

1

Try changing this line:

intable = arcpy.GetParameter(0)

to

intable = arcpy.GetParameterAsText(0)

Also I think you meant this line:

old_field = field_name + "OLD_"

to actually be

old_field = "OLD_" + field_name 

Finally your calculate line should be:

arcpy.CalculateField_management(intable, old_field, '"' + "!" + field.name + "!" + '"')  
  • Thanks Hornbydd. What happens now is the 1st field is added, still no calculation, but now a new message comes up in the background: "The calculated value is invalid for the row with ObjectID = 1. For example, the calculated value may be too large for the field..." This is does not make sense as the fields that were added are identical in format type, size, and precision as the fields they will be copied from. – P K May 13 '14 at 21:34
  • Does field.name need to be wrapped with all the quotations symbols if the values are integers and not strings? – P K May 13 '14 at 21:42
  • Ok, I figured out the cause or the error message/warning "The calculated value is invalid for the row with OBJECTID = 1. etc..." The field length of the non-TEXT fields was too short, which does/did not make any sense, as the field length was copied from the original field!? So instead, all the "OLD_" + field.name fields were set to TEXT and 50 characters long, and the whole thing worked! Now, the only issue is the deletefield_management is not working.... – P K May 15 '14 at 21:52
0

Which version of ArcGIS do you use? Since 10.2 there's the Alter Field tool that allows to rename a field directly. No need to add and calculate a new field and delete the old one. No need to figure out the correct calculate field expression, all field properties are kept and it's super fast even if you have many fields. Works only with geodatabases fc & tables, not shapefiles.

This would be your code:

import arcpy, os
intable = arcpy.GetParameterAsText(0)
fields = arcpy.ListFields(intable)

try:
    for field in fields:
       if not field.required:       # Couldn't help improving this part as well :)
           arcpy.AlterField_management(intable, field.name, field.name + "_OLD")       

except:
    print arcpy.GetMessages()

del intable, fields, field
  • Wow! That is way way better! Thanks. But unfortunately, we are stuck using 10.0, and they don't quite know when they will upgrade to 10.2. It might be after the 1st SP is released. – P K May 15 '14 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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