2

enter image description hereDisclaimer: Python Neophyte, Uber-reliant on GUI, working to wean...

I have a Python script for replacing multiple Field values with another table value that was graciously provided by the inimitable DKlassen and seen here:

import arcpy
tablename = "c:\temp\*NAMEOFYOURTABLE*"
fieldvalues = ['fieldname1','fieldname2','fieldname3'......'fieldnameX']

cur = arcpy.UpdateCursor(tablename)
for row in cur:
    sourcevalue = row.getValue('*name of field where source value coming from*')
    for f in fieldvalues:
        row.setValue(f, sourcevalue)
    cur.updateRow(row)

Question: After running it it seems to replace all the fieldvalues with f and defaults integers to 0 or 1 (since sourcevalues is a real number between 0-1)--how do I include an operator to multiple for f in fieldvalues by a ratio, or real/float number? Obviously the syntax of f*sourcevalue does not work.

3
  • Are you able to reduce your example code (which looks fine) to just one hardwired field in place of 'name of field where source value coming from' and one field instead of your list of fields and see if you get the expected result because fsourcevalue can produce floats so it is may be a field definition preventing that going in. To show fsourcevalue can produce floats try "f = 2 sourcevalue = 1.1 print f*sourcevalue"
    – PolyGeo
    Oct 23, 2013 at 22:35
  • float(f) * float(sourcevalue) will force the result to be a float value. Can you show us a few rows/columns in your table so we can get an better idea of what you are looking for. (or a screenshot).
    – dklassen
    Oct 23, 2013 at 22:49
  • Dklassen: Column A=Column_A* a float number--updating the current field. Poly Geo: what would the syntax be to run that test, where would I insert that snippet? EDIT Figured out screenshot, put it up top. Oct 23, 2013 at 22:58

1 Answer 1

3

Grant Malcolm, If you want to calculate - say Column B01001e1 with the PROPORTION field you would do the following:

cur = arcpy.UpdateCursor("block_senatedist13NAD83ft")
for row in cur:
    row.setValue("B01001e1", row.getValue("B01001e1") * row.getValue("PROPORTION"))
    cur.updateRow(row)

If you want to update multiple fields:

cur = arcpy.UpdateCursor("block_senatedist13NAD83ft")
fieldvalues = ["B01001e1","B01001e2"]
for row in cur:
    for f in fieldvalues:
        row.setValue(f, row.getValue(f) * row.getValue("PROPORTION"))
    cur.updateRow(row)
2
  • Perfect. You're the best dklassen. Oct 24, 2013 at 4:24
  • If you have any other ARCPY questions let me know.
    – dklassen
    Oct 24, 2013 at 15:42

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.