Deleting Empty Fields

04-24-2019 01:34 AM
New Contributor

I am trying to find the empty fields in a feature class and delete the fields.i am checking the number of empty or null rows are equal to total number of rows then i am deleting the field.i don't want to go throw all the rows and check field is empty or null.but many fields has spaces in the records i want to trim the spaces in string fields. i could not fingure it how to add trim function in SearchCursor where_clause or in sql_clause. could you please let me know how to do it.

result = arcpy.GetCount_management(layer)
totalRows = result[0]
if field.type == 'Double':
     query = '("{0}" is null OR "{0}" = 0)'.format(
     query = '("{0}" is null or "{0}"=\'\')'.format( # query = '("{0}" is null or Trim("{0}")=\'\')'.format(

rows = [row for row in arcpy.da.SearchCursor(layer, [], query)]
if int(totalRows) <= len(rows):     
         arcpy.DeleteField_management(layer, [])
0 Kudos
2 Replies
MVP Emeritus

did that code throw an error?  python uses strip() to remove whitespace 

a = " a "


MVP Esteemed Contributor

What back-end DBMS/datastore holds your data?  The support of TRIM and other SQL functions is DBMS/datastore specific.  For example, trying to use TRIM in a WHERE clause with file geodatabases will generate a Python RuntimeError due to invalid expression.

0 Kudos