Calculate new field in multiple tables based on an existing field that is not always present

900
2
Jump to solution
01-30-2019 04:12 PM
JohnPapageorgiou
New Contributor III

I have a set of file geodatabase tables that are an output form another process.  From that output I created a subset based on part of the filename.  I would like to merge all these files together but the merge tool will not work because in some tables a field with the common name "PROJECT" differs in length.  My solution is to add a new field "Project1" and calculate it using the field calculator in arcpy.  Unfortunately the problem I am having with this is that not all of the tables in my subset have the field "PROJECT" so the calculation is giving me the error: ExecuteError: ERROR 000539: Invalid field PROJECT
Failed to execute (CalculateField).

This leads me to believe, that I need to set up some sort of code block with IF ELSE to iterate through this list of tables and ignore tables where the "PROJECT" field is not present and continue.  I need help writing that and figuring out where to place the code block.  Any help is greatly appreciated.  I am using ArcGIS desktop 10.4.1. This is what I have so far, thanks in advance.

import arcpy
import os
from arcpy import env
env.workspace = r"C:\John\test22\Caribou_Plumas_Testing.gdb"
list_NRes_Tables = arcpy.ListTables("*_NRes*")

for myTables in list_NRes_Tables:
    arcpy.AddField_management(myTables,"Project1","TEXT",255,"","","Project1", "NULLABLE", "NON_REQUIRED")
    for myTables in list_NRes_Tables:
        arcpy.CalculateField_management(myTables,"Project1","!PROJECT!","PYTHON")
0 Kudos
1 Solution

Accepted Solutions
RhettZufelt
MVP Frequent Contributor

you can use the ListFields command to get a list of the fields in the current table or featureclass, then the if/then:do something based on the result.

field_names = [f.name for f in arcpy.ListFields(myTables)]
if 'PROJECT' in field_names:
    do the calculation
else:
    do something else‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

also, depending on the size of you data, calculate field can take a long time.

if large data set, consider using arcpy.da.UpdateCursor ( UpdateCursor—Help | ArcGIS Desktop  ).

I updated a few of my scripts a couple weeks back, and instead of the calculations taking 45 minutes, takes about 14 seconds now.

R_

View solution in original post

2 Replies
RhettZufelt
MVP Frequent Contributor

you can use the ListFields command to get a list of the fields in the current table or featureclass, then the if/then:do something based on the result.

field_names = [f.name for f in arcpy.ListFields(myTables)]
if 'PROJECT' in field_names:
    do the calculation
else:
    do something else‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

also, depending on the size of you data, calculate field can take a long time.

if large data set, consider using arcpy.da.UpdateCursor ( UpdateCursor—Help | ArcGIS Desktop  ).

I updated a few of my scripts a couple weeks back, and instead of the calculations taking 45 minutes, takes about 14 seconds now.

R_

JohnPapageorgiou
New Contributor III

Hi Rhett,

My apologies for my delayed response.  Thank you for your response that was the direction I took.  Managing the lists was a bit of a learning curve.  SearchCursor was helpful as well in my instance.  

thanks,

John

0 Kudos