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
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':.

  • 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. – Colin Mar 10 '16 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. – Andy Bradford Mar 10 '16 at 15:46
  • @Colin your post still demonstrates the mistake that Tobson described. Read his answer again. – Tom Mar 10 '16 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. – TobsenB Mar 11 '16 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.