In post earlier today, (Another da.SearchCursor Issue ) through an oversight on my part I couldn't execute a select layer by attributes function. Silly me.... At rate, I've moved past that issue but now faced with constructing a where clause that involves membership in a dictionary.
Essentially, I've constructed a dictionary of street types that looks something like this:
typeDict <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">"AVE"</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"BLVD"</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"CIR"</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">}</SPAN> <SPAN class="comment token">#etc, etc</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN> As best as I can tell, the way to see if a given string is a member of a dictionary, one can :
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> typeDict
<SPAN class="punctuation token">{</SPAN><SPAN class="string token">'BLVD'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'AVE'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'CIR'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">}</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="string token">"AVE"</SPAN> <SPAN class="keyword token">in</SPAN> typeDict
<SPAN class="token boolean">True</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="string token">"ST"</SPAN> <SPAN class="keyword token">in</SPAN> typeDict
<SPAN class="token boolean">False</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="string token">"ST"</SPAN> <SPAN class="operator token">not</SPAN> <SPAN class="keyword token">in</SPAN> typeDict
<SPAN class="token boolean">True</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="string token">"somevalue"</SPAN> <SPAN class="operator token">not</SPAN> <SPAN class="keyword token">in</SPAN> typeDict<SPAN class="punctuation token">:</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"nope"</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN>
nope
<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>Okay, easy enough. The problem is constructing a valid "where clause' for the SelectLayerByAttribute() when considering membership in a dictionary. I've tried dozens of {}.format() approaches but the red font on my screen looks something like the U.S. spend deficit. I need some advice to successfully select a record or records based on the dictionary:
intable <SPAN class="operator token">=</SPAN> "C<SPAN class="punctuation token">:</SPAN>\Path\To\File<SPAN class="punctuation token">.</SPAN>gdb\Table
arcpy<SPAN class="punctuation token">.</SPAN>MakeTableView_management<SPAN class="punctuation token">(</SPAN>intable<SPAN class="punctuation token">,</SPAN><SPAN class="string token">"view"</SPAN><SPAN class="punctuation token">)</SPAN>
typeDict <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">"AVE"</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"BLVD"</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"CIR"</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">}</SPAN> <SPAN class="comment token">#etc, etc</SPAN>
fields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Type"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"StreetName"</SPAN><SPAN class="punctuation token">]</SPAN>
where <SPAN class="operator token">=</SPAN> "StreetName <SPAN class="operator token">>=</SPAN> <SPAN class="string token">'A'</SPAN> <SPAN class="comment token">#just concerned with named streets, no numbers</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><SPAN class="string token">"view"</SPAN><SPAN class="punctuation token">,</SPAN>fields<SPAN class="punctuation token">,</SPAN>where<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>
Select <SPAN class="operator token">=</SPAN> <SPAN class="comment token">#need to format a where clause that</SPAN>
<SPAN class="comment token"># evalutes row[0] not in typeDict</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SelectLayer<SPAN class="punctuation token">,</SPAN>ByAttribute<SPAN class="punctuation token">(</SPAN>view<SPAN class="punctuation token">,</SPAN><SPAN class="string token">"ADD_TO_SELECTION"</SPAN><SPAN class="punctuation token">,</SPAN>Selection<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>jamesfreddyc
bixb0012
Dan_Patterson
(I should just create a signature block with you guys in it; maybe I can convince my boss to put you on retainer....)