1

I have written a script that checks populated (or empty) values off of coded value and range domains. It works well with the range domains. It finds empty fields as well as values that are out of range for that field. It also works well when there are values in TEXT fields that don't conform to the accepted domain codes. However, it does not find NULL (None or "") values in TEXT fields.

Here's the code.

if ftype in txtList:  ##Start coded value domain checks
with arcpy.da.SearchCursor(tbl,['UID',fname,'QAQC_FLAG']) as cur:
    listValues = []  
    for row in cur:
        uid = row[0]
        fieldValue = row[1]
        if fieldValue == None or fieldValue == "":
            insRow = ["VNULL",fname,"","Value not populated with incoming data",uid]
            insCur.insertRow(insRow)
            msg = "Added row {0} to {1}.".format(insRow,qaTbl)
            arcpy.AddMessage(msg)

I can't figure out why it won't identify empty fields in the table. Some of these fields have ALL empty values and the script isn't identifying them.

  • 2
    How do you know it's not identifying the empty field? You're not telling it to do anything – Midavalo Apr 12 '17 at 21:43
  • It could be whitespace in the field, try fieldValue.replace(' ','').replace('\t','') == "" to remove spaces and tabs in the condition. – Michael Stimson Apr 12 '17 at 21:45
  • It should insert insRow into another database table with the values in the row. It should also return a message. – Ken Gaines Apr 12 '17 at 21:47
  • 3
    @KenGaines there's nothing in your code snippet that does either of those. Can you edit to include that, as well as whatever messages you receive – Midavalo Apr 12 '17 at 21:48
  • 1
    Did somebody say pGDB?! Jack Dangermond would be turning in his grave if he were dead! – Adam Apr 12 '17 at 22:16
3

Looks like it could be possible that instead of an empty string "", you may have whitespace (one or more spaces " " etc). Maybe test a little more implicitly for "empty" values:

isEmpty = lambda x: x is not None and str(x).strip() != ""

Then try that in your if statement:

with arcpy.da.SearchCursor(tbl,['UID',fname,'QAQC_FLAG']) as cur:
    listValues = []  
    for row in cur:
        uid = row[0]
        fieldValue = row[1]
        if isEmpty(fieldValue):
            insRow = ["VNULL",fname,"","Value not populated with incoming data",uid]
            # do stuff here

And as others have mentioned, the snippet is not actually doing anything after the if statement.

  • Do you have any documentation for isEmpty? it's not a function that I know and google can't find it - or rather finds too much 'other' stuff. – Michael Stimson Apr 12 '17 at 22:06
  • Yup! This is the problem. I will incorporate this piece into the code and test. Many thanks! – Ken Gaines Apr 12 '17 at 22:06
  • you should be able to use if not fieldValue: to check if the field is empty, otherwise it will contain some white space or whatever else – Adam Apr 12 '17 at 22:19
  • 1
    @MichaelMiles-Stimson isEmpty() is the lambda function I created above the block of code, which I should have clarified that it needs to be in the same code before the cursor. @Adam Not necessarily, doing the implicit falsy test by simply using if not fieldValue: will also test false if the value is 0 if the field is numeric. This may not be the case here, but to quote the Zen of Python explicit is better than implicit – crmackey Apr 13 '17 at 14:01

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.