Python Script for Field Search

591
3
Jump to solution
12-11-2013 09:16 AM
JustinCrowther1
New Contributor
Hello,

I am looking to figure out a python script to automate, in field calculator, looking through multiple fields and providing a Yes if data is present.

Context:
I have a Feature Class for permitted sections. This feature class has multiple permits; and the permits can cover multiple sections. I am looking to create one field that indicates if the section has a permit, regardless of the permit type. I cannot figure out the script to automate this process.

Any suggestions would be greatly appreciated.

Thanks,
Justin
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
You can iterate through the fields and only search the permit fields.  Ex:

fc = "permits"  for field in arcpy.ListFields(fc):     #Specify all none permit fields     if field.name != "OBJECTID" and field.name != "Shape" and field.name != "Shape_Length" and field.name != "Shape_Area" and field.name != "GlobalID" and field.name != "HasPermit":         with arcpy.da.UpdateCursor(fc, [field.name, "HasPermit"], "NOT " + field.name + " IS NULL") as cursor:             #if query is valid, set hasValue variable to 'true'             hasValue = 'true'             if hasValue == 'true':                 for row in cursor:                     row[1] = "YES"                     cursor.updateRow(row)                 hasValue = 'false'  del cursor, row

View solution in original post

0 Kudos
3 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Justin,

You could use the following:

fc = "permits"

with arcpy.da.UpdateCursor(fc, ["Permit1", "Permit2", "HasPermit"], "NOT Permit1 IS NULL OR NOT Permit2 IS NULL") as cursor:
    for row in cursor:
        row[2] = "YES"
        cursor.updateRow(row)

del cursor, row


What the code does is search through the permit fields (i.e. Permit1, Permit2) and if any of them have a value it will populate the 'HasPermit' field with a value of YES. 

If your data does not have NULL values, you will have to update the query to:

NOT Permit1 = '' OR NOT Permit2 = ''
0 Kudos
JustinCrowther1
New Contributor
Jake,

Thanks for the help! I tried that code with some modification to my data and it worked for two fields. Only thing i have and issue with is that I have over 50 fields to search through. Is there a way to stream line this code with out writing out each field multiple times?

Justin
0 Kudos
JakeSkinner
Esri Esteemed Contributor
You can iterate through the fields and only search the permit fields.  Ex:

fc = "permits"  for field in arcpy.ListFields(fc):     #Specify all none permit fields     if field.name != "OBJECTID" and field.name != "Shape" and field.name != "Shape_Length" and field.name != "Shape_Area" and field.name != "GlobalID" and field.name != "HasPermit":         with arcpy.da.UpdateCursor(fc, [field.name, "HasPermit"], "NOT " + field.name + " IS NULL") as cursor:             #if query is valid, set hasValue variable to 'true'             hasValue = 'true'             if hasValue == 'true':                 for row in cursor:                     row[1] = "YES"                     cursor.updateRow(row)                 hasValue = 'false'  del cursor, row
0 Kudos