Select to view content in your preferred language

Calculation error using cursors, second cursor uses first value of first cursor

1136
4
Jump to solution
11-19-2013 06:56 PM
jasonfargo
Emerging Contributor
Attempting to add up values of one field based on the value of another field.  The first cursor adds up the values in the field correctly, but the second one uses the first value of the first cursor resulting in a wrong total.  If I replace the code "totalPrice1 = (Row[1])" with "totalPrice1 = 0.0", it just skips the first value entirely. I think the error lies in this code in the second cursor "totalPrice2 = (Row[1])" because it seems to set the value to the first value in the first cursor instead of the first value in the second.  ideas?

Fields = ['Field1','Dollar Value'] FirstCursor = arcpy.da.SearchCursor(fcName,Fields) for Row in FirstCursor:     totalPrice1 = (Row[1])     for Row in FirstCursor:         if Row[0] == "Example 1":             totalPrice1 += (Row[1]) del Row del FirstCursor  SecondCursor = arcpy.da.SearchCursor(fcName,Fields) for Row in SecondCursor:     totalPrice2 = (Row[1]) # Something needs to be changed here to get the first value correct     for Row in SecondCursor:         if Row[0] == "Example 2":             totalPrice2 += (Row[1]) del Row del SecondCursor
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
T__WayneWhitley
Honored Contributor
Can't you just do something like this?:
Fields = ['Field1','Dollar Value'] totalPrice1 = 0.0 totalPrice2 = 0.0 TheOnlyCursorNeeded = arcpy.da.SearchCursor(fcName,Fields) for Row in TheOnlyCursorNeeded:      if Row[0] == "Example 1":             totalPrice1 += (Row[1])      elif Row[0] == "Example 2":           totalPrice2 += (Row[1])  del Row del TheOnlyCursorNeeded

View solution in original post

0 Kudos
4 Replies
T__WayneWhitley
Honored Contributor
Can't you just do something like this?:
Fields = ['Field1','Dollar Value'] totalPrice1 = 0.0 totalPrice2 = 0.0 TheOnlyCursorNeeded = arcpy.da.SearchCursor(fcName,Fields) for Row in TheOnlyCursorNeeded:      if Row[0] == "Example 1":             totalPrice1 += (Row[1])      elif Row[0] == "Example 2":           totalPrice2 += (Row[1])  del Row del TheOnlyCursorNeeded
0 Kudos
jasonfargo
Emerging Contributor
Can't you just do something like this?:
Fields = ['Field1','Dollar Value']
totalPrice1 = 0.0
totalPrice2 = 0.0
TheOnlyCursorNeeded = arcpy.da.SearchCursor(fcName,Fields)
for Row in TheOnlyCursorNeeded:
     if Row[0] == "Example 1":
            totalPrice1 += (Row[1])
     elif Row[0] == "Example 2":
          totalPrice2 += (Row[1])

del Row
del TheOnlyCursorNeeded


Yes, but then totalPrice2 always has an incorrect value when I do the math manually.  It uses the first value from Example1 then continues to add Example2 values to it.  Example1 values end up correct, but the Example2 values always seem to get that first record wrong which makes the final value incorrect.
0 Kudos
T__WayneWhitley
Honored Contributor
That doesn't make sense - can you post some of your data?

I have not tested further, but the only thing I can think of at this point is possibly an indention error with this block, corrected below (this time I am using tabs):
for Row in TheOnlyCursorNeeded:
 if Row[0] == "Example 1":
  totalPrice1 += (Row[1])
 elif Row[0] == "Example 2":
  totalPrice2 += (Row[1])
0 Kudos
jasonfargo
Emerging Contributor
That doesn't make sense - can you post some of your data?

I have not tested further, but the only thing I can think of at this point is possibly an indention error with this block, corrected below (this time I am using tabs):
for Row in TheOnlyCursorNeeded:
 if Row[0] == "Example 1":
  totalPrice1 += (Row[1])
 elif Row[0] == "Example 2":
  totalPrice2 += (Row[1])


Your suggestion worked perfectly, It was a selection error on my part while troubleshooting.  thank you
0 Kudos