ArcPy & Python – Converting Case of All Values in Table Fields

arcgis-10.1arcpy

Does anyone know how to use the .upper() in a stand alone python script so it can be used to change all characters in a table into UPPER case? I don't want to use the field calculator since I have multiple tables, etc…

I currently am using a cursor to correct mistakes in my tables, but can't figure out the correct syntax to use it to convert all the strings to UPPER case!


The above question would apply equally well when the change of case desired is to lower or title.

Best Answer

The following example shows how to integrate the built-in python method .upper() with the arcpy update cursor. This example first tests if a field is of type String then checks each row within that string for lowercase values. If there are lower case values, the row is updated with all upper case.

import arcpy

fc = r'C:\temp\test.gdb\yourFC'

desc = arcpy.Describe(fc)
fields = desc.fields

for field in fields:
    if field.Type == "String":
        with arcpy.da.UpdateCursor(fc, str(field.name)) as cursor:
            for row in cursor:
                if row[0] == None:  # Check for "<Null>"
                    continue
                else:
                    any(x.islower() for x in row[0]) == True
                    row[0] = row[0].upper()
                    cursor.updateRow(row)