I need a small code in python that will delete records that have empty value in a specific field of the layer. The name of the feature class is WCSCustomer and the name of the field is GROUTE
I found code to strip all non printables on StackOverflow.
def stripper(s): return "".join(i for i in s if ord(i)<127 and ord(i)>32) fc = r"C:\tmp\Test.gdb\WCSCustomer" field = "GROUTE" with arcpy.da.UpdateCursor(fc, field) as rows: for row in rows: try: val = stripper(s) except: val = row[0] if val in ["", None]: rows.deleteRow(row)
I assume you are working with text fields since you are talking about empty being different than Null (or None in Python). When working with text fields, just be aware there are some characters and numerous control characters that don't display (the field will look empty in ArcMap/ArcCatalog) but prevent !field! == "" from being True.
It might be worth looking over the following discussion: I am trying to calculate a field where i want to exclude any null values. For example. Field calulate field X with any value that is not null from field Y. I have been messing with python, but just can not seem to get this to run properly.
or alternately
do the row.getvalue or row[index number] thing ( we will call it x using Mitch's designation)
then....
if x in [None,""]:
delete stuff
The rationale is that an empty records is going to show <null> in the table, but python returns None and shapefiles store "" and other stuff stores one of those two (if memory serves). The nice thing is you can add anything you want to the list. the python 'isinstance' can also be used, but then you have to remember the class and None is a NoneType or which there is only one instance of that class.
Adrian,
I tried to ran it and I get an exception..
Use None instead of "Null"
for x in this:
if x is None:
delete that stuff
maybe try
import arcpy fc = r'C:\tmp\Test.gdb\WCSCustomer' field = "GROUTE" whereClause = field + ' = ""' updCurs = arcpy.UpdateCursor(fc, whereClause) for row in updCurs: if not row.getValue(field): updCurs.deleteRow(row)
Thanks, Adrian. I have just empty value, not Null.. I am not sure if I can change the script for that myself..
Hi Liana,
Take a look at this thread:
Delete Rows where value is Null
I believe this will have the solution that you need.
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.