I have list that contains all the Boat Ramps I want to validate.
I want a query that loops through each boat ramp name and then determines which ones do not have a record in the last 30 days. I need that result to be written to a CSV file.
I think I have the beginning and the end but not the part that loops and queries for Boat Ramps that do NOT have a record in the last 30 days
I need to query the FC variable (my dataset that has all the records)
"Ramp_1" is the field in the FC that has the Boat Ramp Name
Using the List for Boat Ramp names (List)
Loop through the List and find which records in the FC do not have a record in the last 30 days.
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">import</SPAN> os<SPAN class="punctuation token">,</SPAN> string
<SPAN class="keyword token">import</SPAN> smtplib<SPAN class="punctuation token">,</SPAN> shutil
<SPAN class="keyword token">import</SPAN> email<SPAN class="punctuation token">,</SPAN> time
<SPAN class="keyword token">import</SPAN> sys<SPAN class="punctuation token">,</SPAN> csv
<SPAN class="keyword token">from</SPAN> email<SPAN class="punctuation token">.</SPAN>MIMEMultipart <SPAN class="keyword token">import</SPAN> MIMEMultipart
<SPAN class="keyword token">from</SPAN> email<SPAN class="punctuation token">.</SPAN>mime<SPAN class="punctuation token">.</SPAN>text <SPAN class="keyword token">import</SPAN> MIMEText
<SPAN class="comment token">#Variables==============================================</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> <SPAN class="string token">"C:\\Users\\xyz\\AppData\\Roaming\ESRI\\Desktop10.4\\ArcCatalog\\xyz.sde"</SPAN>
fc <SPAN class="operator token">=</SPAN> <SPAN class="string token">"xyz"</SPAN>
var_additionalcomments <SPAN class="operator token">=</SPAN> <SPAN class="string token">'additionalcomments'</SPAN>
var_CreationDate<SPAN class="punctuation token">,</SPAN>var_Creator <SPAN class="operator token">=</SPAN> <SPAN class="string token">'CreationDate'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'Creator'</SPAN>
headers <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'comments'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'Creation Date'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'Creator'</SPAN><SPAN class="punctuation token">]</SPAN>
QueryFields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>var_additionalcomments<SPAN class="punctuation token">,</SPAN>var_CreationDate<SPAN class="punctuation token">,</SPAN>var_Creator
<SPAN class="comment token"># Get Date 30 days ago</SPAN>
<SPAN class="keyword token">import</SPAN> datetime <SPAN class="keyword token">as</SPAN> DT
today <SPAN class="operator token">=</SPAN> DT<SPAN class="punctuation token">.</SPAN>date<SPAN class="punctuation token">.</SPAN>today<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
month_ago <SPAN class="operator token">=</SPAN> today <SPAN class="operator token">-</SPAN> DT<SPAN class="punctuation token">.</SPAN>timedelta<SPAN class="punctuation token">(</SPAN>days<SPAN class="operator token">=</SPAN><SPAN class="number token">30</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> month_ago
<SPAN class="comment token"># Get list of all Boat Ramps</SPAN>
myLayer <SPAN class="operator token">=</SPAN> <SPAN class="string token">"BoatRampsWGS"</SPAN>
myField <SPAN class="operator token">=</SPAN> <SPAN class="string token">"SITENAME"</SPAN>
myBoatRampList <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>myLayer<SPAN class="punctuation token">,</SPAN> myField<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">print</SPAN> myBoatRampList
<SPAN class="comment token"># Loop through List of Boat Ramps and return those that dont have inspection within 30 days</SPAN>
<SPAN class="comment token"># I AM LOST HERE .... THIS IS WHERE THE LOOP AND QUERY SHOULD BE BUT DONT KNOW WHERE TO STATR</SPAN>
<SPAN class="comment token"># Write the results to CSV file.</SPAN>
outFileRegionDate <SPAN class="operator token">=</SPAN> open<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">"E:\ArcGISProjects\xyz\PythonScripts\PythonSync\PythonScripts\z_outFileRegionDate.csv"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"w"</SPAN><SPAN class="punctuation token">)</SPAN>
mylist<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN> QueryFields<SPAN class="punctuation token">,</SPAN> where_clause<SPAN class="operator token">=</SPAN>expressionDate<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursorDate<SPAN class="punctuation token">:</SPAN>
outFileRegionDate<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">','</SPAN><SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>headers<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">'\n'</SPAN><SPAN class="punctuation token">)</SPAN>
flagDate <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">False</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursorDate<SPAN class="punctuation token">:</SPAN>
flagDate <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
zvalDate <SPAN class="operator token">=</SPAN> str<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'{0},{1},{2}'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
outFileRegionDate<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN>zvalDate <SPAN class="operator token">+</SPAN> <SPAN class="string token">"%s\n"</SPAN><SPAN class="punctuation token">)</SPAN>
mylist<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>zvalDate<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> flagDate<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"No rows found"</SPAN>
outFileRegionDate<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</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></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><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></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><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>