Select to view content in your preferred language

using upper() python function in sde gdbs to alter field names of feature class

780
3
Jump to solution
07-16-2023 09:36 PM
KaydenSim
New Contributor II

A feature class that I'm using as an input to a script was clipped from an enterprise SDE geodatabase, and has all of its field names in lower case. The script I'm using references all the field names in uppercase, as the input was previously clipped from a normal geodatabase instead. 

Rather than update every line of the script that references a given field name (the script has about 1000 lines total). I came up with what I thought was a way to change the field names of the input clipped feature class to upper case- my code was as following:

"""def capitalize_sde():
 
    input = "INPUT"
    fl = []
    flist = arcpy.ListFields(input)
    for f in flist:
        if f.name != "Shape_Area" and f.name != "Shape_Length":
            if f.type != "GEOMETRY" and f.type != "OID" and f.type != "BLOB":
                fl.append(f.name)

    for lowerfl in fl:
        upperfl = lowerfl.upper()
        arcpy.AlterField_management(input, lowerfl, upperfl)

 

capitalize_sde()

"""

I thought this would work to change all the field names of my "INPUT" feature class to upper case. Instead, the field names stayed the same, all in lower case letters. When I tested this in the debug console of vs code, it showed that the upper() function worked to my string variable upperfl, but the last line- arcpy.AlterField_management() didn't actually update the field. 

If you know of any way to fix this problem, I'd greatly appreciate it!  

Thanks for any help! 

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Field renaming (as well as renaming featureclasses) wont change the case if there isn't any other changes to the name. To do what you are wanting, append a _1 or something at the end of the uppercased name, alter the field, and then alter again to remove it.  This was from another question I answered some time ago that was after the same thing:

def capitalize_fc_fnames(in_fc):
    # capitalizes all field names for specified feature class
    print("\nCapitalizing all field names for feature class..")
    all_fields = [f.name for f in arcpy.ListFields(in_fc) if f.name not in ["Shape_Area", "Shape_Length", "Shape", "OID", "BLOB"]]
    for fld in all_fields:
        arcpy.management.AlterField(in_fc, fld, new_field_name=f'{fld.upper()}_cmon')
        arcpy.management.AlterField(in_fc, f'{fld.upper()}_cmon', new_field_name=fld.upper())

 

 

View solution in original post

3 Replies
DanPatterson
MVP Esteemed Contributor

You need to provide more information as to what INPUT actually is.

Alter Field (Data Management)—ArcGIS Pro | Documentation

either provide the full path to the featureclass or set the workspace as in the code examples above


... sort of retired...
0 Kudos
by Anonymous User
Not applicable

Field renaming (as well as renaming featureclasses) wont change the case if there isn't any other changes to the name. To do what you are wanting, append a _1 or something at the end of the uppercased name, alter the field, and then alter again to remove it.  This was from another question I answered some time ago that was after the same thing:

def capitalize_fc_fnames(in_fc):
    # capitalizes all field names for specified feature class
    print("\nCapitalizing all field names for feature class..")
    all_fields = [f.name for f in arcpy.ListFields(in_fc) if f.name not in ["Shape_Area", "Shape_Length", "Shape", "OID", "BLOB"]]
    for fld in all_fields:
        arcpy.management.AlterField(in_fc, fld, new_field_name=f'{fld.upper()}_cmon')
        arcpy.management.AlterField(in_fc, f'{fld.upper()}_cmon', new_field_name=fld.upper())

 

 

KaydenSim
New Contributor II

Thanks for the help, altering the field twice after appending something to the end worked!

 

0 Kudos