make all of the data in fields uppercase

5973
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
1 Solution

Accepted Solutions
NeilAyres
MVP Alum

Are you not doing something like this in your code.

# start editing
edit = arcpy.da.Editor(DB)
edit.startEditing(False, False)
edit.startOperation()

View solution in original post

12 Replies
JeffWard
Honored Contributor

You can get to SDE feature classes through your connection file:

"C:\\Users\\<yourusername>\\AppData\\Roaming\\ESRI\\Desktop10.whatever\\ArcCatalog\\SDEConnection.sde"

Jeff Ward
Summit County, Utah
JasonTipton
Frequent Contributor

Put an r in front of that string and you won't have to escape all of the slashes.

r'C:\some\path'

JoshuaBixby
MVP Esteemed Contributor

If you drag a feature class from any source into the interactive Python window of ArcGIS Desktop, it will show you the path.  If the SDE connection file is in your "Database Servers" or "Database Connections" folder in ArcCatalog, the path will be relative to those folder, but the path will still be valid for the currently logged in user.  Jeff Ward​ shows what a full path would look like.

by Anonymous User
Not applicable

I was really excited about this idea. The first time I tried it, it worked like a charm. But now I get the error message, even though I am in an edit session:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 10, in <module>
RuntimeError: The requested operation is invalid on a closed state [Ozark.DBO.BoxCulvert][STATE_ID = 58]

Does anyone have any ideas?

Thank you,

Andrea

JoshuaBixby
MVP Esteemed Contributor

What is your exact syntax?  Your original syntax doesn't show any edit sessions.  The error message indicates you either havent started editing after creating the edit session or you haven't started an operation yet.  The help for arcpy.da.Editor has some really good examples. 

by Anonymous User
Not applicable

So it's sql express SDE geodatabase so I have to start editing because you can't make changes to it outside of an edit session but then I don't know why it's giving me the error message after that. I'm dragging and dropping the feature class from ArcCat to my python window. This code works great when it's in a file gdb. Is there more I have to change because it's in a sql express SDE geodatabase? Thanks.

>>> import arcpy
... fc = r'Database Connections\DEFAULT@Ozark.sde\Ozark.DBO.Storm\Ozark.DBO.BoxCulvert'
... 
... 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)
...                      
... 
... 
0 Kudos
NeilAyres
MVP Alum

Are you not doing something like this in your code.

# start editing
edit = arcpy.da.Editor(DB)
edit.startEditing(False, False)
edit.startOperation()
LukeWebb
Frequent Contributor

If you are running the script from ArcMap, then it should work with the edit session, however you are not referencing the "Layer" in the map, just pointing to it on disk, so that particular instance is not in an edit session!

In the TOC rename the layer to something like "EditLayer"

Then in the code, instead of pointing to the path on disk:

fc = r'Database Connections\DEFAULT@Ozark.sde\Ozark.DBO.Storm\Ozark.DBO.BoxCulvert'

try:

fc = 'EditLayer'

If this doesnt work, you will have to use Arcpy.Mapping to point to the mxd, and then find the layer within the MXD.

Alternatively, as Neil Ayres has said, start an end the edit session in the code! (Then make sure you are not in an edit session in ArcMAP else the layer may be locked)

Hopefully this makes sense and explains what is happening.

by Anonymous User
Not applicable

Yeah, I was originally pointing to it from the TOC also. That doesn't work either. So it's back to my original idea (left over from VBA days) where I thought I had to reference the layer in the mxd somehow. Like you mention. But instead of that I'm going to try and start and end the edit session in the code using the bit that Neil just showed me above. thanks.

0 Kudos