make all of the data in fields uppercase

5977
12
Jump to solution
05-21-2015 12:58 PM
by Anonymous User
Not applicable
>>> import arcpy
... fc = r'O:\path here...\Storm.gdb\Storm\JunctionBox'
... 
... 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)

Hello all,

I have this code that I pieced together in the past with your help. But it references a specific feature class on my network. How can I get it to now reference my sql express geodatabase feature class instead? The feature class is in my mxd where I run this, so can I just reference the feature class within this current map document somehow?

Thank you in advance,

Andrea

0 Kudos
12 Replies
by Anonymous User
Not applicable

Nope, I'm not. I didn't figure I had to since I manually start editing before I run the code. But after starting to read the help it looks like I need something like that.

0 Kudos
by Anonymous User
Not applicable

So three people actually helped me answer this question as I had to look at the help for arcpy.da.Editor to copy the sample code there. Thank you for your help! Code that works below.

import arcpy
import os

fc = 'Database Connections/DEFAULT@Ozark.sde/Ozark.DBO.Storm/Ozark.DBO.BoxCulvert'
workspace = 'Database Connections/DEFAULT@Ozark.sde'

#start an edit session. Must provide the workspace.
edit = arcpy.da.Editor(workspace)

#Edit session is started without an undo/redo stack for versioned data. So True for versioned data.
edit.startEditing(False, True)

#start edit operation
edit.startOperation()


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)


#stop the edit session
edit.stopOperation()

#Stop the edit session and save the changes
edit.stopEditing(True)
ThomasColson
MVP Frequent Contributor

If youre database is a SQL SDE, you can use a function and trigger to do this real-time as data is edited. A slight modification of this SQL Code will do the trick, the modification being you'd have to alter the function to upper-case every character.

0 Kudos