Simple looping using list of CSV values in Python

6200
4
02-09-2012 12:32 PM
RyanUtz
New Contributor
Hello,

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 cannot 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.

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?!?

Here's the code:

import csv, arcpy
sheds = csv.reader(open('E:/Weathering_GIS/Sheds.csv','rb'))

from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")

env.workspace = 'E:/Weathering_GIS/Working.gdb'

for row in sheds:
   print row
   arcpy.MakeFeatureLayer_management('E:/Weathering_GIS/Working.gdb/USGS_Points', 'Shedpoints9')
   arcpy.SelectLayerByAttribute_management("Shedpoints9", "NEW_SELECTION", "GageCode='"+row.insert)  #here's where it's breaking. How can I insert the first of the "sheds"?!? 


Thanks,
Ryan
Tags (2)
0 Kudos
4 Replies
curtvprice
MVP Esteemed Contributor
Thanks for turning me on to the csv module. Cool!
import csv, arcpy
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")

env.workspace = 'E:/Weathering_GIS/Working.gdb'

try:
    f = open('E:/Weathering_GIS/Sheds.csv') 
    sheds = csv.reader(f)
    for row in sheds:
        print row # this is a python list - you want the first element ([0])
        arcpy.MakeFeatureLayer_management(arcpy.workspace, 'Shedpoints9')
        # my solution is to construct the where clause here. Note the escaped single quotes.
        where = "GageCode = \'%s\'" % row[0]
        arcpy.SelectLayerByAttribute_management("Shedpoints9","",where) 
except:
    del sheds
    f.close()
    raise
0 Kudos
RyanUtz
New Contributor
Curtis,

Thanks very much for the feedback. I wasn't anywhere near that. However, I'm still getting the error:

ExecuteError: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).

even if I just cut-and-paste your code.

I'm amazed that this is so challenging...
0 Kudos
curtvprice
MVP Esteemed Contributor
ExecuteError: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).

even if I just cut-and-paste your code.


Well, you didn't want me to test it did you?  Put a "print where" in the code right before you do the selection and maybe that will help you figure out what's up.  To show all non-printables etc you can try: print repr(where).

You didn't show us any data. If GageCode is an integer, then your query expression should be:

where = "GageCode = %s" % int(row[0])
0 Kudos
RyanUtz
New Contributor
Curtis,

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.

Thanks!
Ryan
0 Kudos