[GIS] Using Search Cursor to loop through a table and use as variable

arcpycursorloopsearch

I've a fairly extensive script which requires the user to manually define a variable which populates rest of script such as file paths, file names and outputs etc. Now I'm trying get the scrip to loop through a table of file prefixes (I've these available as polygons geodatabase as well). I thought best way to accomplish this would be using Search Cursors on a table
where I've file prefix saved in each row, so if I were to set up a loop it would grab the row value, substitute it in place of the variable (coName) and then goes through rest of the steps in the script. So I've tried the following, but I'm having a difficult time trying to figure out:
(1) how to tell SC to obtain a value and then insert it as the variable
(2) in other variations it pulls the loops through the table and then ends up using the last row item in the table.

import arcpy,os
from arcpy import env
from arcpy.sa import * 


# Table with prefix file names such as ABCD_10001, ABDC_10002 etc.,
fTable = "C:/test_table.txt"
field = "FileName"

cursor = arcpy.da.SearchCursor(fTable,field)

# Iterate through each rows
row = cursor.next()
while row:
    print (row.getValue(field))
    cursor.next()

    coName = row
    wFolder = "C:/Imagery/"
    coGDB = os.path.join(str(coName) + ".gdb/")

    env.workspace = os.path.join(str(wFolder), str(coGDB))
    print (arcpy.Exists(env.workspace))

    DEV = os.path.join(str(wFolder), str(coGDB), str(coName) + "_DEV")
    print (arcpy.Exists(DEV))

    outRas = Raster(DEV) * 2
    outRas.Save(str(coName) + "_x2")

UPDATE

I'm using ArcGIS 10.4. The last two lines are a psuedo map algebra equation.

Best Answer

I'm not sure what you're doing with the last two lines, so for now I'm leaving them out of the script, but for the rest take a look at the following:

import arcpy,os

# Table with prefix file names such as ABCD_10001, ABDC_10002 etc.,
fTable = "C:/test_table.txt"
field = "FileName"

with arcpy.da.SearchCursor(fTable, field) as cursor:
    for row in cursor:
        print row[0] 

        coName = row[0] # coName is now holding the value of FileName for the current row of your table

        wFolder = r"C:\Imagery"
        coGDB = os.path.join("{}.gdb".format(coName))

        arcpy.env.workspace = os.path.join(wFolder, coGDB)
        print (arcpy.Exists(arcpy.env.workspace))

        DEV = os.path.join(wFolder, coGDB, "{}_DEV".format(coName))
        print arcpy.Exists(DEV)
        print DEV

Therow is the entire contents of a line in your table. row[0] is the first item of that line. If you want to get your filename prefix you need to use row[0].

The value output from the line print dev should be something like C:\Imagery\ABCD_10001.gdb\ABCD_10001_DEV.

As for the last two lines, I'm not sure what you're trying to do here.

outRas = DEV * 2
outRas.Save(str(coName) + "_x2")

Doing a print DEV * 2 will produce C:\Imagery\ABCD_10001.gdb\ABCD_10001_DEVC:\Imagery\ABCD_10001.gdb\ABCD_10001_DEV, and the line outRas.Save probably won't do anything as you're just telling a string variable to save, which will error out.

Related Question