I believe that the benevolent dictator for life is recommending that you not use the string module. You can now use the string methods to accomplish most of what I believe you are trying to do, for instance
gp.CalculateField_management(fc,"name","!name!.title()","PYTHON")
Someone else just asked me the same thing. How about this one-liner?gp.CalculateField_management(fc,"name","string.capwords(!name!)","PYTHON","import string")(You have to import the string module because it's not a built-in string function.)There may be other exceptions such as dashes and apostropies that need Uppercasing such as O'Brien or other Double-Banger names. In this case use a cursor and regular expressionimport arcgisscripting import string import re gp = arcgisscripting.create(9.3) # settings fc = "d:/work/test.gdb/trail" # simple way but inadequate gp.CalculateField_management(fc,"name","string.capwords(!name!)","PYTHON","import string") print gp.GetMessages() # Handle imbedded quotes like O'Brien quoteabbrev = re.compile("'[a-z]") # just as fast as a calculate and we can trap exceptions, errors cur = gp.UpdateCursor(fc) row = cur.next() while row: x = string.capwords(row.name) if quoteabbrev.search(x): print x,name name = quoteabbrev.sub(quoteabbrev.search(x).group(0).upper(),x) cur.updateRow(row) row = cur.next() del row,cur print gp.GetMessages()I never got the VB equivalent ProperCase to work in a script.
import arcgisscripting import string import re gp = arcgisscripting.create(9.3) # settings fc = "d:/work/test.gdb/trail" # simple way but inadequate gp.CalculateField_management(fc,"name","string.capwords(!name!)","PYTHON","import string") print gp.GetMessages() # Handle imbedded quotes like O'Brien quoteabbrev = re.compile("'[a-z]") # just as fast as a calculate and we can trap exceptions, errors cur = gp.UpdateCursor(fc) row = cur.next() while row: x = string.capwords(row.name) if quoteabbrev.search(x): print x,name name = quoteabbrev.sub(quoteabbrev.search(x).group(0).upper(),x) cur.updateRow(row) row = cur.next() del row,cur print gp.GetMessages()
columnValue = "hello world" columnValue.split(" ") returnValue = columnValue[0].capitalize() + " " columnValue[1].capitalize() print returnValue >>>"Hello World"
gp.CalculateField_management(fc,"name","string.cap words(!name!)","PYTHON","import string")
returnValue = columnValue[0].capitalize() + " " columnValue[1].capitalize() gp.CalculateField_management(fc, "field", returnValue, "PYTHON")
updateRows = gp.UpdateCursor("sfora_2") updateRow = updateRows.Next() while updateRow: newString = "" for word in updateRow.MYFIELD.split(" "): newString = word.capitalize() + " " + newString updateRow.MYFIELD = newString[:-1] updateRows.updaterow(updateRow) #<<< THIS WAS THE MISSING LINE! updateRow = updateRows.Next() del updateRow del updateRows
try: .... your update script here... except: print gp.Messages(2) gp.AddError(gp.Messages(2))
updateRows = gp.UpdateCursor("sfora_2") updateRow = updateRows.Next() while updateRow: newString = "" for word in updateRow.MYFIELD.split(" "): newString = word.capitalize() + " " + newString updateRow.MYFIELD = newString[:-1] updateRow = updateRows.Next() del updateRow del updateRows
updateRows = gp.UpdateCursor("sfora_2") updateRow = updateRows.Next() while updateRow: newString = "" for word in updateRow.MYFIELD.split(" "): newString = item.capitalize() + " " + newString updateRow.MYFIELD = newString[:-1] updateRow = updateRows.Next() del updateRow del updateRows
updateRows = gp.UpdateCursor("sfora_2") updateRow = updateRows.Next() while updateRow: updateRow.MYFIELD = updateRow.MYFIELD.upper() #.upper() capitalizes every character updateRow = updateRows.Next() del updateRow del updateRows
gp.CalculateField_management(TABLE, "texx", "str(!texx!).upper()", "PYTHON", "")
gp.CalculateField_management(TABLE, FIELD, "StrConv([texx],vbUpperCase)", "VB")
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.