multiply values from fieldlist into new field

1398
2
Jump to solution
01-18-2012 05:04 AM
ErikMartin
Occasional Contributor
Hi,

I am trying to select a subset of fields and multiply the values into a new field.  I am having no problems creating a list of fields.  From there I'm not sure if I should be using CalculateField_managment  or an UpdateCursor.  Or both?  Any help or direction would be greatly appreciated.  Although I;ve tried several avenues, I haven't gotten very far yet...

import arcpy arcpy.env.workspace = r"K:\Data\PythonTest.gdb" fc = "FeatureClass"  #Create list of fields that begin with "WR_" .. I want to multiply all of these together and write values to a new field "Product" FieldList = arcpy.ListFields(fc, "WR_*", "All") rows = arcpy.UpdateCursor(fc)  for field in FieldList:     fName = field.name     print fName  #Everything up to here works-- I get a list of the fields I want      #Open an update cursor for row in rows:     for field in FieldList:         CurrVal = row.getValue(field)  # this doesn't work & I'm not sure how to proceed from here
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
I think you are just missing some minor things. Take this as an example.

rows = arcpy.UpdateCursor(fc) for row in rows:     Val = 1     for field in fieldList:         CurrVal = row.getValue(field.name)         Val = Val * CurrVal # Or whatever you want to do to the values     row.NewField = Val     rows.updateRow(row)

View solution in original post

0 Kudos
2 Replies
MathewCoyle
Frequent Contributor
I think you are just missing some minor things. Take this as an example.

rows = arcpy.UpdateCursor(fc) for row in rows:     Val = 1     for field in fieldList:         CurrVal = row.getValue(field.name)         Val = Val * CurrVal # Or whatever you want to do to the values     row.NewField = Val     rows.updateRow(row)
0 Kudos
ErikMartin
Occasional Contributor
Thanks, Matthew.  That was exactly what I needed.  Here's my final code, in case anyone else is interested.

import arcpy
arcpy.env.workspace = r"K:\Data\PythonTest.gdb"
fc = "FeatureClass"

#create update Cursor
rows = arcpy.UpdateCursor(fc)

#Create list of fields that begin with "WR_" .. I want to multiply all of these together and write values to a new field "Product"
FieldList = arcpy.ListFields(fc, "WR_*", "All")
for field in FieldList:
    fName = field.name
    print fName
for row in rows:
    Val = 1
    for field in FieldList:
        CurrVal = row.getValue(field.name)
        print CurrVal
        Val = Val * CurrVal
        row.setValue("Product", Val)  #updates values to "product" field
        rows.updateRow(row)
0 Kudos