2

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?

3
  • What type of dataset is the "sourceTable" (i.e. shapefile, file geodatabase, sde...)?
    – JeremyFon
    Commented May 14, 2013 at 14:39
  • 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? Commented May 14, 2013 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
    Commented May 14, 2013 at 15:01

1 Answer 1

5

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
1
  • Perfect! It worked like a charm!!- I didn't use getValue correctly. Thanks a lot Jeremy!!!!! You're a life saver
    – Ann
    Commented May 14, 2013 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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