Bypass Nulls or blanks

821
7
11-07-2018 02:51 PM
CCWeedcontrol
Occasional Contributor III

I have the following and i get 'NoneType' object has no attribute 'strip' on line 10. There are some records that have NULL or Blanks in the ACCOUNT field. How do i bypass or skill the ones that are null or blank?

try:
    #build a dictionary of ACCOUNT : Address pairs
    addDict = {row[0]:row[1] for row in arcpy.da.SearchCursor(fc1,['ACCOUNT','SiteAddress'])}

    #search through fc2 to see if addresses match based on ACCOUNT value
    with arcpy.da.UpdateCursor(fc2, ['Account','SiteAddres','Verifi2']) as cursor:
        for row in cursor:
            if row[1] != None:
                if row[0] in addDict:
                    if row[1].strip().lower()!= addDict[row[0]].strip().lower(): #if the value associated with those ids do not match
                        row[2] = 'No'                
                    else:
                        row[2] = 'Match'
                    cursor.updateRow(row)
    del cursor
0 Kudos
7 Replies
JoshuaBixby
MVP Esteemed Contributor

Stylistically, my_var is None is more Pythonic than my_var == None.  Style aside, you are making sure that row[1] is not None, but you haven't done the same with row[0].  Instead of suggesting you just add another conditional check, what do you want to do when either Account, SiteAddres, or both are NULL in the table?

CCWeedcontrol
Occasional Contributor III

I would like to just bypass them and continue to check for matches.

0 Kudos
JoeBorgione
MVP Emeritus

When is a blank a blank?

''  or ' ' or '   ' or '              '

For those of you old enough to have listened to real vinyl, my blank and <null> rants must sound like a broken record.

Image result for broken record gif

That should just about do it....
DanPatterson_Retired
MVP Emeritus

NONE.... not again

another option in case you have multiple conditions.

if row[1] not in (None, 'None', 'NoneType', '', "", " ", 'nOnE':
    # carry on because there is no None-nonsense
CCWeedcontrol
Occasional Contributor III

What if fc1 has blanks? i guess i didn't think about that till now.

0 Kudos
CCWeedcontrol
Occasional Contributor III

Sorry to bring this back up but i am still having issues.

also the issue with if fc1 doesn't have anything in the "ACCOUNT" field how do you just keep the update cursor going on to the next one?

try:
    #build a dictionary of ACCOUNT : Address pairs
    addDict = {row[0]:row[1] for row in arcpy.da.SearchCursor(fc1,['ACCOUNT','SiteAddress'])}

    #search through fc2 to see if addresses match based on ACCOUNT value
    with arcpy.da.UpdateCursor(fc2, ['Account','SiteAddres','Verifi2']) as cursor:
        for row in cursor:
            if row[1] != None:
                if row[0] in addDict:
                    if row[0] != None:
                        if row[1].strip().lower()!= addDict[row[0]].strip().lower(): #if the value associated with those ids do not match
                            row[2] = 'No'                
                        else:
                            row[2] = 'Match'
                        cursor.updateRow(row)
    del cursor
0 Kudos
DanPatterson_Retired
MVP Emeritus

if row[whatever] != None:

change to,

if row[whatever] is not None:

generally safer.

Now if line 3 doesn't have anything? you want to continue on to where?

because on line 9 checks to see if fc2 has 'Account' in the previously created dictionary... if it doesn't, it just goes on to the next row, which is what is coded, hence there is no update.

Perhaps you can throw in some print statements to illustrate what is being printed at each stage and where you want it to go.

Also, try-except blocks are pretty useless you have some code to handle the 'fail' conditions. What is beyond line 16?