Select to view content in your preferred language

Deleting Empty Fields

1356
2
04-24-2019 01:34 AM
MichaelLorenz
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(field.name)
else:
     query = '("{0}" is null or "{0}"=\'\')'.format(field.name) # query = '("{0}" is null or Trim("{0}")=\'\')'.format(field.name)

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

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

a = " a "

a.strip()

'a'
JoshuaBixby
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