I'm trying to write a function that takes in a feature class and converts all its field names to uppercase, using the Alter Field tool. Here is my code:
def capitalize_fc_fnames(in_fc):
# capitalizes all field names for specified feature class
print("\nCapitalizing all field names for master network output feature class..")
all_fields = [f.name for f in arcpy.ListFields(in_fc)]
for f in all_fields:
arcpy.management.AlterField(in_fc, f, new_field_name=f.upper())
I am running the function on a feature class in a file geodatabase. The function runs without errors, but when I open the feature class, none of the fields have been altered.
I have also tested in Pro using the Alter Field GUI tool on just one field, but with that nothing happens: no errors, no changes to the field names.
This post from 2017 suggests that at that time it was a known but unresolved bug that Alter Field did nothing. I *really* hope that's not still the case as I have 90+ fields I want to convert to uppercase. @BrandonFlessner did you ever find a solution to this? @DanPatterson do you have any sage input on this?
Thanks,
Solved! Go to Solution.
I wonder if this is the extension of how it also has problems when trying to do it in catalog- it wont change cases if the names are the same. Just reverts back to whatever case it was... I'd try appending a temp value and then change it back to the desired name:
def capitalize_fc_fnames(in_fc):
# capitalizes all field names for specified feature class
print("\nCapitalizing all field names for master network output feature class..")
all_fields = [f.name for f in arcpy.ListFields(in_fc)]
for f in all_fields:
arcpy.management.AlterField(in_fc, f, new_field_name=f'{f.upper()}_cmon')
arcpy.management.AlterField(in_fc, f'{f.upper()}_cmon', new_field_name=f.upper())
syntax looks fine, but I would exclude fields that are not editable or required in your list comprehension
Field—ArcGIS Pro | Documentation
The only things that bug me right now are:
Hi Dan,
@Anonymous User's answer seems to work (i.e., change to a different name, then change back to the capitalized version of the original name). But that's still not ideal because it moves the field name to the end.
solved my dilema:
Was
def convert_field_names_to_lowercase(fc):
"""
Converts the field names of a feature class to lowercase.
Args:
fc (str): The path to the feature class.
"""
try:
fields = arcpy.ListFields(fc)
for field in fields:
new_name = field.name.lower()
if new_name != field.name:
arcpy.AlterField_management(fc, field.name, new_name)
print(f"Renamed field: {field.name} to {new_name}")
Now is
if new_name != field.name and field.name not in ["OBJECTID","Shape_Length","Shape_Area","GlobalID", "Shape"]:
arcpy.AlterField_management(fc, field.name, temp_name)
print(f"Renamed field: {field.name} to {temp_name}")
arcpy.AlterField_management(fc, temp_name, new_name)
print(f"Renamed field: {temp_name} to {new_name}")
print("---")
I wonder if this is the extension of how it also has problems when trying to do it in catalog- it wont change cases if the names are the same. Just reverts back to whatever case it was... I'd try appending a temp value and then change it back to the desired name:
def capitalize_fc_fnames(in_fc):
# capitalizes all field names for specified feature class
print("\nCapitalizing all field names for master network output feature class..")
all_fields = [f.name for f in arcpy.ListFields(in_fc)]
for f in all_fields:
arcpy.management.AlterField(in_fc, f, new_field_name=f'{f.upper()}_cmon')
arcpy.management.AlterField(in_fc, f'{f.upper()}_cmon', new_field_name=f.upper())
Hi Jeff,
This seems to work after testing out in the Pro python window. This approach also seems to move the field to the end, which is not ideal. I might end up using it if nothing else comes to mind.
This might be a view/cache thing, or under the hood (more likely) it is creating a new field with the new name, and then doing a migrate from the old field so that would put them at the end. If you could, can you create a new FC and field map the data to the uppercased fields? then save over the old?