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 have a feature class in which one field (let's call it "Field_A"), as yet empty, is to be populated by the contents of another field ("Field_B") multiplied by a certain number (I'll call it the "constant"). Would it be a good idea to simply populate Field_A using an update cursor, as follows:

rows = arcpy.UpdateCursor(InTable) 
for row in rows:
     row.Field_A = row.Field_B * constant
     rows.updateRow(row) 
del row, rows

, or do I have to use the field calculator? If I have to use the field calculator, I'm having an issue with the syntax. I defined the expression thus:

expression = "Field_A * constant"

and the code is as follows:

arcpy.CalculateField_management(InTable, "Field_A", expression)

When using CalculateField as shown, the script runs without errors, but "Field_A" is populated with zeros as a result. What am I doing wrong with CalculateField, or should I simply use an update cursor?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

I think you could go either way with this code. Your first code block would run correctly.
I imagine there are some speed differences between looping through rows and doing a calculate values, so the latter is probably much faster.

Try this for the field calculator expression:

expression = "!Field_B! * constant"

arcpy.CalculateField_management(InTable, "Field_A", expression, "PYTHON_9.3")

You did not have the required field delimiter for the field name in the expression string. Fields have to be wrapped with a "!", just like they would appear in the Field Calculator window in ArcMap when using a python expression. Also note that I added PYTHON_9.3 to the end, which will invoke the more current version of the geoprocessor. Read the Calculate Field (Data Management) help entry, for more information.

Here is a screenshot of the Field Calculator window for reference: Field Calculator Window

share|improve this answer
    
Although this option makes sense, a field name does not have to be wrapped with a "!" in order for the field calculator python method to work correctly within a python script. –  artwork21 May 14 '13 at 15:52
    
@artwork21 - Based on the help document, in the Expression block, every time a field name is referenced, it is wrapped in "!", so that is what I went by. –  Get Spatial May 14 '13 at 15:57
    
Thanks! I used the field calculator, with exclamation marks and the expression type, and everything worked this time. –  Hattifnatt May 14 '13 at 16:13

Based on your question you want to multiple values from field B not field A. Your expression should be:

expression = "Field_B * constant"
share|improve this answer

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.