Hello.
I am building a script to run as an ArcGIS tool that takes a line file and does a bunch of stuff to it, then exports a table and uses that table to make route events.
I do not need, nor do I want, all of the fields from the original line file in the layer events attributes table. As such, I have been working on a way to export only the necessary fields from the feature class to a table for use in the make route even layers tool.
Here is what I have so far:
<SPAN class="comment token">#Hide all fields that are not required for samples.</SPAN>
keepFieldList <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"SAMPLE_ID"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"SEGMENT_ID"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NAME"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"LENGTH"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"WIDTH"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"BEGIN_MP"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"END_MP"</SPAN><SPAN class="punctuation token">]</SPAN>
fieldInfo <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>FieldInfo<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
fieldList <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFields<SPAN class="punctuation token">(</SPAN>sampevents<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> field <SPAN class="keyword token">in</SPAN> fieldList<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> field<SPAN class="punctuation token">.</SPAN>name <SPAN class="keyword token">in</SPAN> keepFieldList<SPAN class="punctuation token">:</SPAN>
fieldInfo <SPAN class="operator token">=</SPAN> fieldInfo <SPAN class="operator token">+</SPAN> field<SPAN class="punctuation token">.</SPAN>Name <SPAN class="operator token">+</SPAN> <SPAN class="string token">" "</SPAN> <SPAN class="operator token">+</SPAN> field<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">+</SPAN> <SPAN class="string token">"VISIBLE;"</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
fieldInfo <SPAN class="operator token">=</SPAN> fieldInfo <SPAN class="operator token">+</SPAN> field<SPAN class="punctuation token">.</SPAN>Name <SPAN class="operator token">+</SPAN> <SPAN class="string token">" "</SPAN> <SPAN class="operator token">+</SPAN> field<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">+</SPAN> <SPAN class="string token">"HIDDEN;"</SPAN>
<SPAN class="comment token">#Export sample events to table for use in make route event layer.</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>TableToTable_conversion<SPAN class="punctuation token">(</SPAN>sampevents<SPAN class="punctuation token">,</SPAN> gisdata<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Sample_Events_Table"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Here is the traceback info (Note that line 157 in the full script is line 9 in the above portion):
Traceback (most recent call last):
File "C:\Projects\CreateSamples\CreateSamples.py", line 157, in <module>
fieldInfo = fieldInfo + field.Name + " " + field.name + "HIDDEN;"
AttributeError: 'Field' object has no attribute 'Name'
Failed to execute (CreateSamples).
Thanks for any help resolving this. I am not entirely sure what the error is saying. My idea here is to hide all fields not in the keepFieldList and then export those fields to a new table. Then that table will be used as the event table input in the make route events tool.