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")
I'm using ArcGIS 10.4. The last two lines are a psuedo map algebra equation.
row.getValue(field)yet. Also, I just noticed this but should
cursor.next()be after the map algebra? So this way it knows that after it has retrieved the value, used it as a variable and performed spefic steps...It needs to move on to the next row.
row.getValue()does not exist in DA cursors. Using the
with/forlooping construct saves you on using
next()(which should be at the top or bottom)