I'm new to Python and need a sample ArcPy script which would allow me to query a FC attribute in a .gdb and output the result to my C drive. My FC is name 'Approaches' and the attribute I need to query on is 'Approved' and the value in that attribute I want to pull is the "Yes" value. I would like to export that list of Approved Approaches to my C drive. I would also like to print the Name attribute value of those Approved Approaches.
A basic approach would look something like:
<SPAN class="keyword token">import</SPAN> arcpy fc <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"C:\Path\To\database.gdb\Approaches"</SPAN> <SPAN class="comment token"># feature class</SPAN> fn <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"C:\Path\To\output.txt"</SPAN> <SPAN class="comment token"># save file name</SPAN> f <SPAN class="operator token">=</SPAN> open<SPAN class="punctuation token">(</SPAN>fn<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'w'</SPAN><SPAN class="punctuation token">)</SPAN> fields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Name'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Approved'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># can add other fields such as Approved, if needed</SPAN> whereClause <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Approved = 'Yes'"</SPAN> <SPAN class="comment token"># may need to modify, based on field type, how Y-N is stored</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> fields<SPAN class="punctuation token">,</SPAN> where_clause<SPAN class="operator token">=</SPAN>whereClause<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">:</SPAN> f<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"{}\t{}"</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><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Name and Approved - to file</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"{}\t{}"</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> <SPAN class="comment token"># Name and Approved - to console if needed</SPAN> f<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Done"</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>
(It will probably need some editing to work with your data.)
Approved is a string. I was able to correct the script with a couple changes. Below is the finished script and again thanks for all your help!
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> datetime arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN> points <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"G:\GIS\Anthony\Workspaces\Public Works\ApprovedApproachesTEST\New File Geodatabase.gdb\Approaches"</SPAN> <SPAN class="comment token"># feature class</SPAN> outpath <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"G:\GIS\Anthony\Workspaces\Public Works\ApprovedApproachesTEST\New File Geodatabase.gdb"</SPAN> <SPAN class="comment token"># location of .gdb to create new export fc</SPAN> today <SPAN class="operator token">=</SPAN> datetime<SPAN class="punctuation token">.</SPAN>date<SPAN class="punctuation token">.</SPAN>today<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># creating date time variable</SPAN> dte <SPAN class="operator token">=</SPAN> today<SPAN class="punctuation token">.</SPAN>strftime<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'%m/%d/%Y'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># converts date time variable to string</SPAN> dtep <SPAN class="operator token">=</SPAN> today<SPAN class="punctuation token">.</SPAN>strftime<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'%m_%d_%Y'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># takes date time string variable and replaces / with underscores to use in export fc name</SPAN> fields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Name'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Approved'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Date_Approved'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># define fields for search cursor and print format to use</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>MakeFeatureLayer_management<SPAN class="punctuation token">(</SPAN>points<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Approaches'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#makes approaches fc a feature layer for use in this script</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByAttribute_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Approaches'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NEW_SELECTION"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Approved = 'Yes' AND Date_Approved = CURRENT_DATE"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># creates a selection of approved approaches from the current date</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>FeatureClassToFeatureClass_conversion<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Approaches'</SPAN><SPAN class="punctuation token">,</SPAN> outpath<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Approved_Approaches_{}'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>dtep<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># exports the selection to the outpath variable location</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>points<SPAN class="punctuation token">,</SPAN> fields<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Approved = 'Yes' AND Date_Approved = CURRENT_DATE"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"{}\t{}\t{}"</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> <SPAN class="string token">" Approved"</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="keyword token">print</SPAN> <SPAN class="string token">" "</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Export complete!"</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>
I modified the where clause to pull the current date but it is not successful. I'm not sure what i'm doing wrong but the script runs fine without the where clause date added.
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> datetime arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN> fc <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"G:\GIS\Anthony\Workspaces\Public Works\ApprovedApproachesTEST\New File Geodatabase.gdb\Approaches"</SPAN> <SPAN class="comment token"># feature class</SPAN> fn <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"G:\GIS\Anthony\Workspaces\Public Works\ApprovedApproachesTEST\output.txt"</SPAN> <SPAN class="comment token"># save file name</SPAN> today <SPAN class="operator token">=</SPAN> datetime<SPAN class="punctuation token">.</SPAN>date<SPAN class="punctuation token">.</SPAN>today<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> dte <SPAN class="operator token">=</SPAN> today<SPAN class="punctuation token">.</SPAN>strftime<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'%m/%d/%Y'</SPAN><SPAN class="punctuation token">)</SPAN> f <SPAN class="operator token">=</SPAN> open<SPAN class="punctuation token">(</SPAN>fn<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'w'</SPAN><SPAN class="punctuation token">)</SPAN> fields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Name'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Approved'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># can add other fields such as Approved, if needed</SPAN> whereClause <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Approved = 'Yes' AND Date_Approved = {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>dte<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># may need to modify, based on field type, how Y-N is stored</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> fields<SPAN class="punctuation token">,</SPAN> where_clause<SPAN class="operator token">=</SPAN>whereClause<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">:</SPAN> f<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"{}\t{}"</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><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Name and Approved - to file</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"{}\t{}"</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> <SPAN class="comment token"># Name and Approved - to console if needed</SPAN> f<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Done"</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>
Add it to your where clause. The SQL syntax may vary slightly depending on the type of database you are using. Are you using a file geodatabase or another type?
whereClause <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Approved = 'Yes' AND DateField BETWEEN '2018-01-01' AND '2018-07-01'"</SPAN> whereClause <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Approved = 'Yes' AND DateField > '2018-01-01' AND DateField < '2018-07-01'"</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
One last thing to I forgot to mention was how could I modify that script to only pull approved approaches within a set time frame?
I'm learning slowly in between other projects and YouTube has been a big help. Thanks for the reply!
I should have time this week or next to give this a try. I appreciate your help on this.
Python can be a bit of a learning curve for beginners. The previous script should help but take a look at the documentation so you can understand what the script is doing.
arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN> fields<SPAN class="punctuation token">,</SPAN> where_clause<SPAN class="operator token">=</SPAN>whereClause<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
Documentation: http://pro.arcgis.com/en/pro-app/arcpy/data-access/searchcursor-class.htm
An earlier knowledge article about how to learn Python and ArcPy: https://community.esri.com/groups/technical-support/blog/2014/03/26/7-easy-ways-learning-python-arcpy
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.