Capitalizing Fields

3466
6
07-26-2011 06:22 AM
MaxSwiatkowski
New Contributor
Hi Everyone:

I'm hoping to get some assistance from minds that are better than my own.
I'm new to python but I'm trying to cobble together a script that will let me capitalize first letters in certain fields of shapefiles that I have.

Here is my code:

import sys, string, os, arcgisscripting
gp = arcgisscripting.create(9.3)
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")
gp.Workspace = "C:/Max/Bathymetry_Cleanup/vector/zone17"

gp.OverWriteOutput = True

vectorlist = gp.ListFeatureClasses ("17*", "all")

for vector in vectorlist:
    updateRows = gp.UpdateCursor(vector)
    updateRow = updateRows.Next()
    while updateRow:
           newString = ""
           for word  in updateRow.Units.split(" "):
              newString = word.capitalize() + " " + newString
           updateRow.Units = newString[:-1]
           updateRows.updaterow(updateRow)
    updateRow = updateRows.Next()
    del updateRow
    del updateRows
   
    updateRows = gp.UpdateCursor(vector)
    updateRow = updateRows.Next()
    while updateRow:
       newString = ""
       for word  in updateRow.Units.split(" "):
          newString = word.capitalize() + " " + newString
       updateRow.Srvy_Meth = newString[:-1]
       updateRows.updaterow(updateRow)
    updateRow = updateRows.Next()
    del updateRow
    del updateRows
   
    updateRows = gp.UpdateCursor(vector) 
    updateRow = updateRows.Next()
    while updateRow:
        newString = ""
        for word  in updateRow.Creat_Meth.split(" "):
            newString = word.capitalize() + " " + newString
        updateRow.Creat_Meth = newString[:-1]
        updateRows.updaterow(updateRow)
    updateRow = updateRows.Next()
    del updateRow
    del updateRows
   
    gp.CalculateField_management(vector, "Accuracy", "str(!Accuracy!).upper()", "PYTHON", "")

When I try to run this process, I get an error reading:

Traceback (most recent call last):
  File "C:\Max\python.py", line 17, in <module>
    updateRows = gp.UpdateCursor(vector)
RuntimeError: ERROR 999999: Error executing function.
General function failure

I also tried running it with quotation marks around vector but that gives me the following error:

Traceback (most recent call last):
  File "C:\Max\python.py", line 17, in <module>
    updateRows = gp.UpdateCursor("vector")
RuntimeError: ERROR 999999: Error executing function.
A locator with this name does not exist.

Thanks for any help,

Max
0 Kudos
6 Replies
BruceNielsen
Regular Contributor
Use the title() method to capitalize the first letter of each word in a string. For example, 'abc def'.title() returns 'Abc Def'.
0 Kudos
MaxSwiatkowski
New Contributor
Hi, thanks for the help!

However, I'm now getting an error about a locator not existing, and I've triple checked that my location path is correct. Is there any way to deal with that?
0 Kudos
BruceNielsen
Regular Contributor
Could you post your updated code, preferrably within a CODE block (click the # above the text entry window).
0 Kudos
MaxSwiatkowski
New Contributor
Here's my new code, although I managed to figure out the problem with capitalizing on my own.
Now I'm trying to figure out how to put an if statement into the field calculator without using VBA (my python shell doesn't run it for some reason).
Thanks for any help Bruce.
import sys, string, os, arcgisscripting

gp = arcgisscripting.create(9.3)

gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

gp.Workspace = "C:/Max/Bathymetry_Cleanup/Vector/zone17"

gp.OverWriteOutput = 1

vectorlist = gp.ListFeatureClasses ("17*", "all")

for vector in vectorlist:
    print vector
    gp.CalculateField_management("C:/Max/Bathymetry_Cleanup/Vector/zone17/" + vector, "Units", "str(!Units!).title()", "PYTHON_9.3", "")
    gp.CalculateField_management("C:/Max/Bathymetry_Cleanup/Vector/zone17/" + vector, "Units", "str(!Units!).replace('Meters', 'Metres')", "PYTHON_9.3", "")
    gp.CalculateField_management("C:/Max/Bathymetry_Cleanup/Vector/zone17/" + vector, "Srvy_Meth", "str(!Srvy_Meth!).title()", "PYTHON_9.3", "")
    gp.CalculateField_management("C:/Max/Bathymetry_Cleanup/Vector/zone17/" + vector, "Creat_Meth", "str(!Creat_Meth!).title()", "PYTHON_9.3", "")
    gp.CalculateField_management("C:/Max/Bathymetry_Cleanup/Vector/zone17/" + vector, "Accuracy", "str(!Accuracy!).capitalize()", "PYTHON_9.3", "")
    gp.CalculateField_management("C:/Max/Bathymetry_Cleanup/Vector/zone17/" + vector, "Depth", "abs(!Depth!)*-1", "PYTHON_9.3", "")
    gp.AddField_management("C:/Max/Bathymetry_Cleanup/Vector/zone17/" + vector, "Metres", "DOUBLE", "6", "1", "", "", "NON_NULLABLE", "NON_REQUIRED", "")
    gp.CalculateField_management("C:/Max/Bathymetry_Cleanup/Vector/zone17/" + vector, "Metres", 'if str(!Units!) = "Metres": (!Metres!)=(!Depth!)', "PYTHON_9.3", "")
    gp.CalculateField_management("C:/Max/Bathymetry_Cleanup/Vector/zone17/" + vector, "Metres", 'if str(!Units!) = "Feet": (!Metres!)=0.3048*(!Depth!)', "PYTHON_9.3", "")
    gp.CalculateField_management("C:/Max/Bathymetry_Cleanup/Vector/zone17/" + vector, "Metres", 'if str(!Units!) = "Fathoms": (!Metres!)=1.8288*(!Depth!)', "PYTHON_9.3", "")
0 Kudos
BruceNielsen
Regular Contributor
Max,

I think you were on the right track earlier when you were using an Update Cursor. And as someone else stated recently, leave CalculateField to the modelbuilders. This code should do what you were trying to accomplish:
import arcgisscripting
gp = arcgisscripting.create(9.3)

gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

gp.Workspace = "C:/Max/Bathymetry_Cleanup/Vector/zone17"

gp.OverWriteOutput = 1

vectorlist = gp.ListFeatureClasses ("17*", "all")

for vector in vectorlist:
    print vector
    gp.AddField_management(vector, "Metres", "DOUBLE", "6", "1", "", "", "NON_NULLABLE", "NON_REQUIRED", "")
    rows = gp.UpdateCursor(vector)
    row = rows.Next()
    while row:
        row.Units = row.Units.title().replace('Meters', 'Metres')
        row.Srvy_Meth = row.Srvy_Meth.title()
        row.Creat_Meth = row.Creat_Meth.title()
        row.Accuracy = row.Accuracy.capitalize()
        row.Depth = abs(row.Depth) * -1.0
        if row.Units == "Metres":
            row.Metres = row.Depth
        elif row.Units == "Feet":
            row.Metres = row.Depth * 0.3048
        elif row.Units == "Fathoms":
            row.Metres = row.Depth * 1.8288
        rows.UpdateRow(row)
        row = rows.Next()

Hopefully, I interpreted your logic correctly, and all you will find are syntax errors.

Good luck
0 Kudos
DaleHoneycutt
Regular Contributor
See also this blog post about using Calculate Field.  It contains examples of capitalizing values.
0 Kudos