<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Calculate new field in multiple tables based on an existing field that is not always present in Python Snippets Questions</title>
    <link>https://community.esri.com/t5/python-snippets-questions/calculate-new-field-in-multiple-tables-based-on-an/m-p/756009#M53</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a set of file geodatabase tables that are an output form another process.&amp;nbsp; From that output I created a subset based on part of the filename.&amp;nbsp; 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.&amp;nbsp; My solution is to add a new field "Project1" and calculate it using the field calculator in arcpy.&amp;nbsp; 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&lt;BR /&gt;Failed to execute (CalculateField).&lt;/P&gt;&lt;P&gt;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.&amp;nbsp; I need help writing that and figuring out where to place the code block.&amp;nbsp; Any help is greatly appreciated.&amp;nbsp; I am using ArcGIS desktop 10.4.1. This is what I have so far, thanks in advance.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; arcpy
&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; os
&lt;SPAN class="keyword token"&gt;from&lt;/SPAN&gt; arcpy &lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; env
env&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;workspace &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; r&lt;SPAN class="string token"&gt;"C:\John\test22\Caribou_Plumas_Testing.gdb"&lt;/SPAN&gt;
list_NRes_Tables &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;ListTables&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"*_NRes*"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;

&lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; myTables &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; list_NRes_Tables&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;AddField_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;myTables&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"Project1"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"TEXT"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;255&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;""&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;""&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"Project1"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"NULLABLE"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"NON_REQUIRED"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    &lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; myTables &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; list_NRes_Tables&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
        arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;CalculateField_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;myTables&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"Project1"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"!PROJECT!"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"PYTHON"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 08:04:24 GMT</pubDate>
    <dc:creator>JohnPapageorgiou</dc:creator>
    <dc:date>2021-12-12T08:04:24Z</dc:date>
    <item>
      <title>Calculate new field in multiple tables based on an existing field that is not always present</title>
      <link>https://community.esri.com/t5/python-snippets-questions/calculate-new-field-in-multiple-tables-based-on-an/m-p/756009#M53</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a set of file geodatabase tables that are an output form another process.&amp;nbsp; From that output I created a subset based on part of the filename.&amp;nbsp; 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.&amp;nbsp; My solution is to add a new field "Project1" and calculate it using the field calculator in arcpy.&amp;nbsp; 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&lt;BR /&gt;Failed to execute (CalculateField).&lt;/P&gt;&lt;P&gt;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.&amp;nbsp; I need help writing that and figuring out where to place the code block.&amp;nbsp; Any help is greatly appreciated.&amp;nbsp; I am using ArcGIS desktop 10.4.1. This is what I have so far, thanks in advance.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; arcpy
&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; os
&lt;SPAN class="keyword token"&gt;from&lt;/SPAN&gt; arcpy &lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; env
env&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;workspace &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; r&lt;SPAN class="string token"&gt;"C:\John\test22\Caribou_Plumas_Testing.gdb"&lt;/SPAN&gt;
list_NRes_Tables &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;ListTables&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"*_NRes*"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;

&lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; myTables &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; list_NRes_Tables&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;AddField_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;myTables&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"Project1"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"TEXT"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;255&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;""&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;""&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"Project1"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"NULLABLE"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"NON_REQUIRED"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    &lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; myTables &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; list_NRes_Tables&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
        arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;CalculateField_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;myTables&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"Project1"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"!PROJECT!"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"PYTHON"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 08:04:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/calculate-new-field-in-multiple-tables-based-on-an/m-p/756009#M53</guid>
      <dc:creator>JohnPapageorgiou</dc:creator>
      <dc:date>2021-12-12T08:04:24Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate new field in multiple tables based on an existing field that is not always present</title>
      <link>https://community.esri.com/t5/python-snippets-questions/calculate-new-field-in-multiple-tables-based-on-an/m-p/756010#M54</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;field_names &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;f&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;name &lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; f &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;ListFields&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;myTables&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;if&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'PROJECT'&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; field_names&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    do the calculation
&lt;SPAN class="keyword token"&gt;else&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    do something &lt;SPAN class="keyword token"&gt;else&lt;/SPAN&gt;‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;also, depending on the size of you data, calculate field can take a long time.&lt;/P&gt;&lt;P&gt;if large data set, consider using arcpy.da.UpdateCursor (&amp;nbsp;&lt;A class="link-titled" href="http://desktop.arcgis.com/en/arcmap/10.4/analyze/arcpy-data-access/updatecursor-class.htm" title="http://desktop.arcgis.com/en/arcmap/10.4/analyze/arcpy-data-access/updatecursor-class.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;UpdateCursor—Help | ArcGIS Desktop&lt;/A&gt;&amp;nbsp; ).&lt;/P&gt;&lt;P&gt;I updated a few of my scripts a couple weeks back, and instead of the calculations taking 45 minutes, takes about 14 seconds now.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;R_&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 08:04:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/calculate-new-field-in-multiple-tables-based-on-an/m-p/756010#M54</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2021-12-12T08:04:27Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate new field in multiple tables based on an existing field that is not always present</title>
      <link>https://community.esri.com/t5/python-snippets-questions/calculate-new-field-in-multiple-tables-based-on-an/m-p/756011#M55</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Rhett,&lt;/P&gt;&lt;P&gt;My apologies for my delayed response.&amp;nbsp; Thank you for your response that was the direction I took.&amp;nbsp; Managing the lists was a bit of a learning curve.&amp;nbsp; SearchCursor was helpful as well in my instance. &amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thanks,&lt;/P&gt;&lt;P&gt;John&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 06 Mar 2019 22:51:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/calculate-new-field-in-multiple-tables-based-on-an/m-p/756011#M55</guid>
      <dc:creator>JohnPapageorgiou</dc:creator>
      <dc:date>2019-03-06T22:51:16Z</dc:date>
    </item>
  </channel>
</rss>

