1

I am attempting to Search Cursor through a joined table. (I do not have access to the original separated tables)

If the value of "COMPANY" and "DATE_" are then they will be skipped and it is suppose to pull the values from "COLLECTED_" (aka company) and "COLLECTION" (aka Date). Unfortunately it proceeding to overwrite the values with Null.

TOP: New Table of Result

BOTTOM: Original Joined Table of Data

enter image description here

import arcpy as ARCPY
from arcpy import env as ENV
#Environment(s)
workspaceTEST = r"C:\Users\Jim\Desktop\TESTSTRM_SWR.gdb"
ENV.workspace = workspaceTEST

#Variables
pipes15096 = workspaceTEST + "\PIPES_15096"
pipesOfficial = workspaceTEST + "\OFFICIAL_PIPES2015"
fieldsPipesOfficial = ["ID_NUMBER", "DATE_","COMPANY","COLLECTION","COLLECTED_"]

First Attempt:

valueDictOfficialPipes = {pipesRow[0]:(pipesRow[1:]) for pipesRow in ARCPY.da.SearchCursor(pipesOfficial, fieldsPipesOfficial)}
fieldsPipes15096 = ["ID"  , "COLLECTION_DATE" , "COLLECTED_BY"]
with ARCPY.da.UpdateCursor(pipes15096, fieldsPipes15096) as updateRows_Pipes15096:  
    for updateRow2 in updateRows_Pipes15096:  
        keyValue2 = updateRow2[0]
        print "keyValue2:             ", keyValue2
        print "updateRow2:            ", updateRow2        
        if keyValue2 in valueDictOfficialPipes:  
            print "keyValue2 -V2:         ", keyValue2

            if valueDictOfficialPipes[1] and valueDictOfficialPipes[3] != ("<Null>" or " "):
                for nullNoMore in range (1,len(fieldsPipes15096)): 
                    updateRow2[nullNoMore] = valueDictOfficialPipes[keyValue2][nullNoMore-1]  
                    print "updateRow2[nullNoMore]:  ",updateRow2[nullNoMore]
            updateRows_Pipes15096.updateRow(updateRow2)
            print "updateRow2:  ",updateRow2
del valueDictOfficialPipes

I attempted to split into to separate dictionaries and ran them, but the result was cause the second dictionary to overwrite the correction by the first.

#replace fieldsPipesOfficial with the following:    
fieldsPipesOfficial_1 = ["ID_NUMBER", "DATE_",      "COMPANY",]
#                        (ID)         (DATE)       (Collected By)     
fieldsPipesOfficial_2 = ["ID_NUMBER", "COLLECTION","COLLECTED_"]

Second Attempt

valueDictOfficialPipes_1 = {pipesRow[0]:(pipesRow[1:]) for pipesRow in ARCPY.da.SearchCursor(pipesOfficial, fieldsPipesOfficial_1)}
fieldsPipes15096 = ["ID"  , "COLLECTION_DATE" , "COLLECTED_BY"]
with ARCPY.da.UpdateCursor(pipes15096, fieldsPipes15096) as updateRows_Pipes15096:  
    for updateRow2 in updateRows_Pipes15096:  
        keyValue2 = updateRow2[0]
        print "keyValue2:             ", keyValue2
        print "updateRow2:            ", updateRow2        
        if keyValue2 in valueDictOfficialPipes_1:  
            print "keyValue2 -V2:         ", keyValue2

            if valueDictOfficialPipes_1[1] == ("<null>" or " "):
                print "THERE SHOULD BE NO CHANGE"           
            else:
                for nullNoMore in range (1,len(fieldsPipes15096)): 
                    updateRow2[nullNoMore] = valueDictOfficialPipes_1[keyValue2][nullNoMore-1]  
                    print "updateRow2[nullNoMore]:  ",updateRow2[nullNoMore]
            updateRows_Pipes15096.updateRow(updateRow2)
            print "updateRow2:  ",updateRow2

====================================

valueDictOfficialPipes_2 = {pipesRow[0]:(pipesRow[1:]) for pipesRow in ARCPY.da.SearchCursor(pipesOfficial, fieldsPipesOfficial_2)}
fieldsPipes15096 = ["ID"  , "COLLECTION_DATE" , "COLLECTED_BY"]
with ARCPY.da.UpdateCursor(pipes15096, fieldsPipes15096) as updateRows_Pipes15096:  
    for updateRow2 in updateRows_Pipes15096:  
        keyValue2 = updateRow2[0]
        print "keyValue2:             ", keyValue2
        print "updateRow2:            ", updateRow2        
        if keyValue2 in valueDictOfficialPipes_2:  
            print "keyValue2 -V2:         ", keyValue2
            if valueDictOfficialPipes_2[1] == ("<null>" or " "):
                print "THERE SHOULD BE NO CHANGE"           
            else:
                for nullNoMore in range (1,len(fieldsPipes15096)): 
                    updateRow2[nullNoMore] = valueDictOfficialPipes_2[keyValue2][nullNoMore-1]  
                    print "updateRow2[nullNoMore]:  ",updateRow2[nullNoMore]
            updateRows_Pipes15096.updateRow(updateRow2)
            print "updateRow2:  ",updateRow2

del valueDictOfficialPipes_1
del valueDictOfficialPipes_2
1

1 Answer 1

2

If these are true Null fields and not strings with the word "<Null>", then you need to use None in Python for Null. Try:

        if valueDictOfficialPipes_2[1] in [None, "", " "]:
            for nullNoMore in range (1,len(fieldsPipes15096)): 
                updateRow2[nullNoMore] = valueDictOfficialPipes_2[keyValue2][nullNoMore-1]  
                print "updateRow2[nullNoMore]:  ",updateRow2[nullNoMore]
            updateRows_Pipes15096.updateRow(updateRow2)

Edit: I extended the code to show how you have to write this section to make only records that have no value be overwritten and ignore all other rows. The updateRow method should only run if a row is actually updated, so it only runs if the if clause is true.

4
  • I'd also include an empty string "" as these are not Null.
    – Midavalo
    Commented May 4, 2016 at 0:11
  • I added "" to the list. Commented May 4, 2016 at 0:14
  • Richard, Thank you that helps, but it is still overwriting the values with <Null> values. I am attempting to make it recognize that if there is any values other than [None, "", " "] then it should ignore/skip that row and only replace the rows which contain a [None, "", " "]
    – Beau
    Commented May 4, 2016 at 16:36
  • Then you wrote your code logic in reverse, since what you wrote will ignore all rows with [None, "", " "] and overwrite all other rows. Don't include any else clause with this if statement and just do the overwrite code you have written if it is true. indent your updateRow method one level since it only needs to run if the clause is true. Commented May 4, 2016 at 16:54

Your Answer

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

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