Select to view content in your preferred language

Arcpy Update Rows with Field Name

4332
12
Jump to solution
12-07-2018 02:25 PM
EddyClark
Occasional Contributor

I have sixteen different feature classes that mostly have the same fields that share the same domain (Pass/Fail).  I want to create a script that (1) creates a new FC bringing these all together,

      New_Feature_Class field examples: [FirstFailure], [SecondFailure], and [ThirdFailure]. 

then (2) populates rows with Field Name IF [Field] = Fail.

If a row from one of the original 16 feature classes have no fields with a 'Fail', then none of the new [Failure] fields would get populated.  

I've created the first part (below), I just can't even imagine how the second part happens.

import arcpy
from arcpy import *
import os

arcpy.env.overwriteOutput = True

database = "C:\\etc"

arcpy.env.workspace = database

fc_list = arcpy.ListFeatureClasses()

for fc in fc_list:
    fieldname = "From_FC_Name"
    arcpy.AddField_management(fc, fieldname, "TEXT", field_length = 20)
    with arcpy.da.UpdateCursor(fc, fieldname) as cursor:
        for row in cursor:
            if row[0] == None:
                row[0] = str(fc)
            cursor.updateRow(row)

arcpy.ClearEnvironment("workspace")

final_fc = "path"
arcpy.CreateFeatureclass_management(final_fc)
arcpy.Append_management(fclist2, final_fc)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
12 Replies
JoshuaBixby
MVP Esteemed Contributor

Unless you post some sample data that is running into the issues, I can't troubleshoot anymore.  There is a limit to how well you can describe the data and also how I can troubleshoot the code without the data when you are new to scripting.

0 Kudos
EddyClark
Occasional Contributor

OK - sorry to be such a pest.  Your code as written is gold.  I got this to work by adding a [FourthFailure] field.  There was ONE inspection that failed four inspections.  Thanks again Joshua Bixby‌ and Randy Burton‌ for so much help. 

0 Kudos
RandyBurton
MVP Alum

What does the "UGI_Pass_Fail" domain look like?  You need to match the value of the code, not the description.  And the match is case-sensitive.  If "FAIL" is both the code and the description, you will need to change the case use in line 41 of Joshua Bixby's code.

Also, you can use List Fields to find the fields that use a specific domain:

domain = 'UGI_Pass_Fail'
chkFields = []

fc = 'MyFeature' # a feature in the geodatabase
fields = arcpy.ListFields(fc)
for field in fields:
    if field.domain == domain:
        chkFields.append(field.name)

print chkFields‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍