Quantcast
Channel: Question and Answer » arcpy
Viewing all articles
Browse latest Browse all 767

arcpy.setValue for Condition from Dictionary

$
0
0

Hi: I am trying to create a function to update multiple fields based on different criteria. I want to pass the field name and condition thru’ a dictionary (please see the function description in the enclosed code).

def CalculateFieldsBasedOnCondition(sourceTable, fieldNames_Condition):
  '''
  fieldNames_Condition = {'TestID': '"LOC-"+(row.getValue("OBJECTID"))',
                          'UPADATED':'Yes'} 

  usage: CalculateFieldsBasedOnCondition(sourceTable, fieldNames_Condition)
  '''
  try:
    rows=arcpy.UpdateCursor(sourceTable)
    for row in rows:
      for fieldName, condition in fieldNames_Condition.iteritems():
        row.setValue(fieldName,condition)
        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 and the sourceTable is a FileGDB Featureclass.

I would like to get the result of TestID field as “LOC-101″ where 101 is the value of the ObjectID field. If I run this now-it’ll write the condition as such as it’s treated as a string. How can I parse the condition as a non string item?

Any help is appreciated!!!
Thanks in advance!!


Viewing all articles
Browse latest Browse all 767

Trending Articles