<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Simple looping using list of CSV values in Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/simple-looping-using-list-of-csv-values-in-python/m-p/418408#M32855</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for turning me on to the csv module. Cool!&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import csv, arcpy
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")

env.workspace = 'E:/Weathering_GIS/Working.gdb'

try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; f = open('E:/Weathering_GIS/Sheds.csv') 
&amp;nbsp;&amp;nbsp;&amp;nbsp; sheds = csv.reader(f)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in sheds:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row # this is a python list - you want the first element ([0])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(arcpy.workspace, 'Shedpoints9')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # my solution is to construct the where clause here. Note the escaped single quotes.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where = "GageCode = \'%s\'" % row[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("Shedpoints9","",where) 
except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; del sheds
&amp;nbsp;&amp;nbsp;&amp;nbsp; f.close()
&amp;nbsp;&amp;nbsp;&amp;nbsp; raise
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 18:55:44 GMT</pubDate>
    <dc:creator>curtvprice</dc:creator>
    <dc:date>2021-12-11T18:55:44Z</dc:date>
    <item>
      <title>Simple looping using list of CSV values in Python</title>
      <link>https://community.esri.com/t5/python-questions/simple-looping-using-list-of-csv-values-in-python/m-p/418407#M32854</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm valiantly attempting to teach myself Python scripting, and not enjoying this very much so far... All I am trying to do is import a list file (.csv) that corresponds to a field in a shapefile and batch-process some basic GIS functions in a loop. I &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;cannot&lt;/SPAN&gt;&lt;SPAN&gt; figure out how to call up the .csv entry in a list for use in an arcpy command. Forgive me if this is a very basic problem, but I've scoured the web for hours trying to figure this out and am getting nowhere fast. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When one is for-looping with arcpy commands, how in the world do you get Python to accept the value in the loop?? Below I can successfully import the .csv list, get one arcpy command (MakeFeatureLayer_management) to go, but then when I want to insert the first value from the .csv list (in the SelectLayerByAttribute_management command) , nothing I try satisfies Python. What am I doing wrong here?!?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's the code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import csv, arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;sheds = csv.reader(open('E:/Weathering_GIS/Sheds.csv','rb'))&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;from arcpy import env&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;from arcpy.sa import *&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.CheckOutExtension("Spatial")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;env.workspace = 'E:/Weathering_GIS/Working.gdb'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;for row in sheds:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; print row&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management('E:/Weathering_GIS/Working.gdb/USGS_Points', 'Shedpoints9')&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("Shedpoints9", "NEW_SELECTION", "GageCode='"+row.insert)&amp;nbsp; #here's where it's breaking. How can I insert the first of the "sheds"?!?&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Ryan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Feb 2012 20:32:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/simple-looping-using-list-of-csv-values-in-python/m-p/418407#M32854</guid>
      <dc:creator>RyanUtz</dc:creator>
      <dc:date>2012-02-09T20:32:12Z</dc:date>
    </item>
    <item>
      <title>Re: Simple looping using list of CSV values in Python</title>
      <link>https://community.esri.com/t5/python-questions/simple-looping-using-list-of-csv-values-in-python/m-p/418408#M32855</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for turning me on to the csv module. Cool!&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import csv, arcpy
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")

env.workspace = 'E:/Weathering_GIS/Working.gdb'

try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; f = open('E:/Weathering_GIS/Sheds.csv') 
&amp;nbsp;&amp;nbsp;&amp;nbsp; sheds = csv.reader(f)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in sheds:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row # this is a python list - you want the first element ([0])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(arcpy.workspace, 'Shedpoints9')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # my solution is to construct the where clause here. Note the escaped single quotes.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where = "GageCode = \'%s\'" % row[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("Shedpoints9","",where) 
except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; del sheds
&amp;nbsp;&amp;nbsp;&amp;nbsp; f.close()
&amp;nbsp;&amp;nbsp;&amp;nbsp; raise
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:55:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/simple-looping-using-list-of-csv-values-in-python/m-p/418408#M32855</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T18:55:44Z</dc:date>
    </item>
    <item>
      <title>Re: Simple looping using list of CSV values in Python</title>
      <link>https://community.esri.com/t5/python-questions/simple-looping-using-list-of-csv-values-in-python/m-p/418409#M32856</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Curtis,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks very much for the feedback. I wasn't anywhere near that. However, I'm still getting the error:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ExecuteError: ERROR 000358: Invalid expression&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (SelectLayerByAttribute).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;even if I just cut-and-paste your code. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm amazed that this is so challenging...&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Feb 2012 21:10:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/simple-looping-using-list-of-csv-values-in-python/m-p/418409#M32856</guid>
      <dc:creator>RyanUtz</dc:creator>
      <dc:date>2012-02-09T21:10:06Z</dc:date>
    </item>
    <item>
      <title>Re: Simple looping using list of CSV values in Python</title>
      <link>https://community.esri.com/t5/python-questions/simple-looping-using-list-of-csv-values-in-python/m-p/418410#M32857</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;ExecuteError: ERROR 000358: Invalid expression&lt;BR /&gt;Failed to execute (SelectLayerByAttribute).&lt;BR /&gt;&lt;BR /&gt;even if I just cut-and-paste your code. &lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Well, you didn't want me to &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;test&lt;/SPAN&gt;&lt;SPAN&gt; it did you?&amp;nbsp; Put a "print where" in the code right before you do the selection and maybe that will help you figure out what's up.&amp;nbsp; To show all non-printables etc you can try: print repr(where).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You didn't show us any data. If GageCode is an integer, then your query expression should be:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;where = "GageCode = %s" % int(row[0])&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Feb 2012 21:18:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/simple-looping-using-list-of-csv-values-in-python/m-p/418410#M32857</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2012-02-09T21:18:53Z</dc:date>
    </item>
    <item>
      <title>Re: Simple looping using list of CSV values in Python</title>
      <link>https://community.esri.com/t5/python-questions/simple-looping-using-list-of-csv-values-in-python/m-p/418411#M32858</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Curtis, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Wow... very perceptive guess on what was the problem. Indeed, "GageCode" was an integer. Thanks for helping; perhaps I'm not going about this the right way, but I've taught myself other languages in the past and always gotten along.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Ryan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Feb 2012 21:26:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/simple-looping-using-list-of-csv-values-in-python/m-p/418411#M32858</guid>
      <dc:creator>RyanUtz</dc:creator>
      <dc:date>2012-02-09T21:26:47Z</dc:date>
    </item>
  </channel>
</rss>

