3

The for loop below works without error, but the result is get in the table of my database is wrong. Its as if the for loop skips right over the first 3 if and elif statements and goes to the else. The table I am using has values 1 through 5 in each row of the WWFLOWDATA column and the data type is a number. Does anyone have any idea why its not giving me the output I am looking for? Maybe an issue with the access database?

import os 
import arcpy 
from arcpy import env
arcpy.env.workspace = r"C:\Users\sutton\Documents\Python\PythonExcercise\DuncanvilleData.mdb"
try:
 #create new field to hold new values
 arcpy.AddField_management("Wastewater_Lines","PipeAssesment","TEXT","50")
 print("Rehab Field added to Wastewater_Lines")
 with arcpy.da.UpdateCursor("Wastewater_Lines",["WWFLOWDATA","PipeAssesment"]) as cursor:
 cntr = 1
 for row in cursor:
 #update the indicator field
 if row[0] == '5':
 row[1] = "Critical"
 elif row[0] == '4':
 row[1] = "Monitor"
 elif row[0] <= '3':
 row[1] = "Unknown"
 else:
 row[1] = "Unknown"
 cursor.updateRow(row) 
except Exception as e:
 print(e.message)
StatusofScript = "Script executed"
print StatusofScript
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Mar 10, 2016 at 15:12

1 Answer 1

5

You said that the datatype of the DB column is a number, but in your if and elif statements you compare that number to a string (e.g. 4 == '4') which will return False. Maybe you get the desired result when you remove the single quotes around the numbers, for example:

row[0] == 4:

instead of

row[0] == '4':.

answered Mar 10, 2016 at 15:22
4
  • My mistake, that was something I was trying earlier. I have edited the post to reflect what I have more accurately. The problem is I am only getting "Unknown" as a result in the new column despite the other if statements. Commented Mar 10, 2016 at 15:28
  • 2
    Try to open the dataset in ArcMap and try to access the same data through the Python window. Use a SearchCursor with the same parameters and print row[1]. This will help you get a handle on how Python is actually seeing your data. I think @TobsenB is on the right track - I bet if you remove the single quotes around the numbers, you'll get the right results. Commented Mar 10, 2016 at 15:46
  • @Colin your post still demonstrates the mistake that Tobson described. Read his answer again. Commented Mar 10, 2016 at 15:56
  • You're welcome! In the first version of my answer I used the term parantheses instead of quotes - sorry if that led to confusions. Commented Mar 11, 2016 at 7:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.