Sunday, 25 February 2018

arcgis 10.1 - Converting case of all values in all fields of table using ArcPy & Python?


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.



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 ""
continue
else:
any(x.islower() for x in row[0]) == True
row[0] = row[0].upper()
cursor.updateRow(row)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...