Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Changing field names in python script

$
0
0
Hello All,

The short and sweet version is this:

I want to:
  1. Read the list of fields in a shapefile
  2. check for a field starting with "Pri*"
  3. If it is not named "Priority", I want to rename them to "Priority"

I use the Priority field as the Case field in the next tool (summary stats) and
some of the shapefiles have the field as "Priority_1" or something similar.


Code:

prifield = arcpy.ListFields(preparedfeatures, 'Pri*')
        try:
                if prifield != "Priority":
                        arcpy.AddWarning('The priority field was named ' + str(prifield) + " and will be renamed.")
                        arcpy.AddField_management(preparedfeatures, "Priority", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
                        arcpy.CalculateField_management(preparedfeatures, "Priority", prifield , "PYTHON_9.3", "")
        except:
                arcpy.AddWarning(arcpy.GetMessages())
                continue


What prints in the console is this:
"The priority field was named [<Field object at 0x19b708f0[0x18f993b0]>] and will be renamed."

So clearly it is not reading the field names properly.


I have read about arcpy.Alterfield_management, but it reportedly does not work with shapefiles.

I have also tried iterating over the fields using the field index and looking for the field name I want, but no luck.
Thanks for your help.

Viewing all articles
Browse latest Browse all 2485

Trending Articles