Hello:
I am trying to run the select attribute tool for a layer that I created. I can manually run the tool in ArcMap, but when I write the script the code fails after the copy feature tool was used. Not sure what I am doing wrong, I am using the ArcMap help section to get a general idea of the code that I needed.
I am new to python, so I appreciate any guidance anyone has:
<SPAN class="comment token">#Import a CSV file that will show you what ASOS weather stations are in the state.</SPAN>
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">import</SPAN> os
<SPAN class="comment token">#Lets you write over the shape file if needed</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> <SPAN class="string token">"C:\\GEOG485\\FinalProject"</SPAN>
<SPAN class="comment token">#Latitude and Longitude Coordinates </SPAN>
xFieldName <SPAN class="operator token">=</SPAN> <SPAN class="string token">'LON'</SPAN>
yFieldName <SPAN class="operator token">=</SPAN> <SPAN class="string token">'LAT'</SPAN>
<SPAN class="comment token">#Output folder</SPAN>
outFolder <SPAN class="operator token">=</SPAN> <SPAN class="string token">"C:\\GEOG485\\FinalProject\\Output"</SPAN>
<SPAN class="comment token">#ASOS stations layer</SPAN>
eventLayer <SPAN class="operator token">=</SPAN> <SPAN class="string token">"ASOS"</SPAN>
outPutlayer <SPAN class="operator token">=</SPAN> <SPAN class="string token">"C:\\GEOG485\\FinalProject\\FinalProject.gdb\\CurrentStations"</SPAN>
stateLayer <SPAN class="operator token">=</SPAN> <SPAN class="string token">"C:\\GEOG485\\FinalProject\\FinalProject.gdb\\State_ASOS"</SPAN>
<SPAN class="comment token">#Spatial Reference</SPAN>
spatialRef <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>SpatialReference<SPAN class="punctuation token">(</SPAN><SPAN class="number token">4326</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#CSV file that will be imported into ArcMap</SPAN>
csvFilePath <SPAN class="operator token">=</SPAN> <SPAN class="string token">"C:\\GEOG485\\FinalProject\\isd-history.csv"</SPAN>
<SPAN class="comment token">#Select state of interest</SPAN>
targetState <SPAN class="operator token">=</SPAN> <SPAN class="string token">'NJ'</SPAN>
<SPAN class="comment token">#Date of file last updated</SPAN>
targetEnd <SPAN class="operator token">=</SPAN> <SPAN class="string token">'20190329'</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token">#import csv file using the xy event layer tool</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>MakeXYEventLayer_management<SPAN class="punctuation token">(</SPAN>csvFilePath<SPAN class="punctuation token">,</SPAN> xFieldName<SPAN class="punctuation token">,</SPAN> yFieldName<SPAN class="punctuation token">,</SPAN> eventLayer<SPAN class="punctuation token">,</SPAN> spatialRef<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"success1"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Copy features from the XY Event Layer in order to asign Object ID's to the rows in the file</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CopyFeatures_management<SPAN class="punctuation token">(</SPAN>eventLayer<SPAN class="punctuation token">,</SPAN> outPutlayer<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"success2"</SPAN><SPAN class="punctuation token">)</SPAN>
stateQuery <SPAN class="operator token">=</SPAN> <SPAN class="string token">'"STATE" = '</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"'"</SPAN> <SPAN class="operator token">+</SPAN> targetState <SPAN class="operator token">+</SPAN> <SPAN class="string token">"'"</SPAN>
<SPAN class="comment token">#Select Attribute management to narrow down the state of interest</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByAttribute_management<SPAN class="punctuation token">(</SPAN>outPutlayer<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NEW_SELECTION"</SPAN><SPAN class="punctuation token">,</SPAN> stateQuery<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"success4"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByAttribute_management<SPAN class="punctuation token">(</SPAN>outPutlayer<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'SUBSET_SELECTION'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'"END" = '</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"'"</SPAN> <SPAN class="operator token">+</SPAN> targetEnd <SPAN class="operator token">+</SPAN> <SPAN class="string token">"'"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"success5"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SaveToLayerFile_management<SPAN class="punctuation token">(</SPAN>outPutlayer<SPAN class="punctuation token">,</SPAN> stateLayer<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"error2"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
mxd <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MapDocument<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"C:\\GEOG485\\FinalProject\\basemap.mxd"</SPAN><SPAN class="punctuation token">)</SPAN>
df <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListDataFrame<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"*"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
newLayer <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>Layer<SPAN class="punctuation token">(</SPAN>outPutlayer<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>Layer<SPAN class="punctuation token">(</SPAN>df<SPAN class="punctuation token">,</SPAN> newLayer<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Bottom"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">except</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"error4"</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></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>