How to Delete NULL Records

1005
8
04-07-2014 03:20 AM
H_A_D_Padmasiri
New Contributor
Dear Sir

I want to update "CNT_PT_ID" field & delete the NULL records ("CNT_PT_ID" is NULL). I wrote a python script.When it runs "CNT_PT_ID" is update correctly. But "CNT_PT_ID" = "Null" records not deleted. No error message display. Can you give me a help to correct it.

Thanks


Padmasiri
Tags (2)
0 Kudos
8 Replies
WilliamCraft
MVP Regular Contributor
Try changing CNT_PT_ID" = "Null" to be "CNT_PT_ID" IS NULL instead.  Does that work?
0 Kudos
H_A_D_Padmasiri
New Contributor
Try changing CNT_PT_ID" = "Null" to be "CNT_PT_ID" IS NULL instead.  Does that work?


No it does not work it gives following error
Traceback (most recent call last):
  File "J:\Gampaha\Scripts\Script1.py", line 109, in <module>
    if row.CNT_PT_ID is NULL:
NameError: name 'NULL' is not defined

Failed to execute (LISTool1.0).

Thanks
0 Kudos
benberman
Occasional Contributor
It should look something like this:

cur = arcpy.UpdateCursor("CNT_Anno")
for row in cur:
        val1 = row.getValue("Some attribute field")
        if val1.isdigit():
                if int(val1) >= 9000:
                        row.setValue("CNT_PT_ID", int(val1))
                        cur.updateRow(row)
        if row.isNull("CNT_PT_ID")==True:
                cur.deleteRow(row)
del cur
del row


you may also be able to get away with deleting the isdigit() line.
0 Kudos
Zeke
by
Regular Contributor III
Python also often uses None instead of Null.
0 Kudos
MathewCoyle
Frequent Contributor
Give this a try.

layer = r'CNT_Anno'
cur = arcpy.da.UpdateCursor(layer, ['TextString', 'CNT_PT_ID'])
for row in cur:
    if row[0]:
        try:
            if int(row[0]) >= 9000:
                row[1] = int(row[0])
                cur.updateRow(row)
        except:
            pass
    if row[1] is None:
        cur.deleteRow()
0 Kudos
H_A_D_Padmasiri
New Contributor
It should look something like this:

cur = arcpy.UpdateCursor("CNT_Anno")
for row in cur:
        val1 = row.getValue("Some attribute field")
        if val1.isdigit():
                if int(val1) >= 9000:
                        row.setValue("CNT_PT_ID", int(val1))
                        cur.updateRow(row)
        if row.isNull("CNT_PT_ID")==True:
                cur.deleteRow(row)
del cur
del row


you may also be able to get away with deleting the isdigit() line.

 
Dear FLBB

It gives me Following Error

Traceback (most recent call last):
  File "J:\Gampaha\Scripts\Script1.py", line 109, in <module>
    if row.isNULL("CNT_PT_ID")==True:
  File "c:\program files\arcgis\desktop10.1\arcpy\arcpy\arcobjects\_base.py", line 28, in __getattr__
    raise AttributeError("%s" % attr)
AttributeError: isNULL

Failed to execute (LISTool1.0).
Thanks
0 Kudos
benberman
Occasional Contributor
Dear FLBB

It gives me Following Error

Traceback (most recent call last):
  File "J:\Gampaha\Scripts\Script1.py", line 109, in <module>
    if row.isNULL("CNT_PT_ID")==True:
  File "c:\program files\arcgis\desktop10.1\arcpy\arcpy\arcobjects\_base.py", line 28, in __getattr__
    raise AttributeError("%s" % attr)
AttributeError: isNULL

Failed to execute (LISTool1.0).
Thanks


You typed "isNULL". You need to be typing "isNull". Case sensitivity very important in python.
0 Kudos
AhmedEl-Sisi
Occasional Contributor III
This approach should work for you
if not row.CNT_PT_ID :
        cur.deleteRow(row)
0 Kudos