Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to figure out how to use fieldName.title() method while updating rows using updateCursor for multiple fields. I was trying to improve the code of this post to fit my purpose:

arcpy.UpdateCursor for multiple fields

def ChangetoTitleCase(sourceTable, fieldNames):
      try:
        rows=arcpy.UpdateCursor(sourceTable)
        for row in rows:
          for fieldName in fieldNames:
            uField='"%s"' % fieldName
            row.setValue(uField, "uField.title()")
            rows.updateRow(row)
        del row, rows

      except Exception, e:
        import traceback, sys
        tb = sys.exc_info()[2]
        print "Line %i" % tb.tb_lineno
        print e.message

I am working with Python 2.6.5, Arcpy 10 and Win 7 64. Please let me know where I am wrong? Thanks in advance for any help!!!

share|improve this question
    
What type of dataset is the "sourceTable" (i.e. shapefile, file geodatabase, sde...)? –  JeremyFon May 14 '13 at 14:39
    
It's a FeatureClass from FileGDB. Thanks! –  Ann May 14 '13 at 14:47
    
Just to be clear here, is it correct that you are trying to set the value of the attribute to be equal to the attribute name? –  Get Spatial May 14 '13 at 14:55
    
No, I am trying to titleCase the contents of the field. Something which I did -and not working was rows.getValue(uField).title(). So, I tried to use the same method used in labelexpressions i.e. !FieldName!.title(). In this case !FieldName! is uField. Did I make sense? –  Ann May 14 '13 at 15:01
add comment

1 Answer

up vote 5 down vote accepted

Try changing this line as follows:

def ChangetoTitleCase(sourceTable, fieldNames):
  try:
    rows=arcpy.UpdateCursor(sourceTable)
    for row in rows:
      for fieldName in fieldNames:
        newValue = (row.getValue(fieldName)).title()
        row.setValue(fieldName, newValue)
        rows.updateRow(row)
    del row, rows

  except Exception, e:
    import traceback, sys
    tb = sys.exc_info()[2]
    print "Line %i" % tb.tb_lineno
    print e.message
share|improve this answer
    
Perfect! It worked like a charm!!- I didn't use getValue correctly. Thanks a lot Jeremy!!!!! You're a life saver –  Ann May 14 '13 at 15:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.