0

I am attempting to update a table so that a field, "ALLREADY", fills with "Y" or "N" depending upon if "dmready" equals "total_records" AND if "dm_stat" is equal to "Ready for DM". When I run my script, all records fill with N, even those that meet the conditions (as you can see from the first record in the screenshot below).

Screenshot of table

My script:

inTable = os.path.join(env.workspace, "pivotTableTEMP")
with arcpy.da.UpdateCursor(inTable, ["dmready", "total_records", "dm_stat", "ALLREADY"]) as cursor:
    for row in cursor:
        if row[0] == row[1] and row[2] == "Ready for DM":
            row[3] = "Y"
            cursor.updateRow(row)
        else:
            row[3] = "N"
            cursor.updateRow(row)

I have double checked that all fields in my script match the column names and that Ready for DM does not have any hidden characters.

  • Your logic looks correct other than the updateRow method can be outside of the if/else cond. You do not have any returns in the dm_stat values? Are both your number fields type numeric/short? – artwork21 Jul 21 '16 at 13:31
  • Are the fields dmready and total_records both the same type? Also double check that you don't have any trailing spaces in the dm_stat column. – Evan Jul 21 '16 at 14:24
  • What kind of table (DBF, PGDB, FGDB, etc.)? if FGDB, is the database compressed? Have you tried arcpy.UpdateCursor (not da)? – Bjorn Jul 21 '16 at 15:15
  • 1
    Does the dm_stat field have a Coded Value Domain assigned (i.e., does it present a drop down list of choices when you edit that field)? If it does the domain actual value could be something different from the value displayed in the field. Use the Search Layer by Attribute dialog, click on the dt_stat field in the field list, and press the Get Unique Values button to see the actual value store in the field that you need to check for next to the domain translation shown to the user. – Richard Fairhurst Jul 21 '16 at 21:54
1

Try adding a few print statements to verify your values while testing. Something like

for row in cursor:
    print "dmready = '{}'".format(row[0])
    print "total_records = '{}'".format(row[1])
    print "dm_stat = '{}'".format(row[2])
    if row[0] == row[1] and row[2] == "Ready for DM":
        row[3] = "Y"
        cursor.updateRow(row)
    else:
        row[3] = "N"
        cursor.updateRow(row)

This way you can see what your if statement is seeing and possibly why it's not assessing it as True. The reason the values are surrounded with quotes - '{}' - in the print statement is to make it much easier to see if there are extra characters.

  • Thank you Midavalo! This was a great way to see where I was going wrong. My issue ended up being that the dm_stat field had coded values for the domain. – mmoore Jul 22 '16 at 12:06

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.