AnsweredAssumed Answered

Python script that will create a list of a shapefiles values in a field.

Question asked by zancona1987 on Apr 26, 2013
Latest reply on Apr 30, 2013 by mahunter243
Hello everyone,

I have been searching around these forums for awhile now trying to track something down that could help me solve the problem that I am having.

Basically I have 800~ rasters that I can get an output for in the correct order in a list. I need to then take these 800 rasters and multiply each one by a value in a shapefiles attribute table under the field Sum_Count.

I was able to get a printed list of the values that I wanted by using a SearchCursor and a rows.next() function. The issue is that this was only printing out the values for me and not creating a list from these values that I can then use in a calculation.

My code is very sloppy right now as I am VERY new with python so I apologize. My boss just asked me to help him with this and I have been going off things I have found on the internet.

import arcpy, os from arcpy import env from arcpy.sa import *  arcpy.CheckOutExtension("Spatial")  env.workspace = r"E:\Models_Ken\PSI_Outputs\Test_Small_Run"  list1 = []  lstRasters = arcpy.ListRasters("*") for raster in lstRasters:     list1.append(env.workspace + os.sep + raster)     print 'Successfully created list1'     print list1  #env.workspace = r"E:\Models_Ken\PSI_Outputs\Test_Small_Run"  featureClass = r"E:\Models_Ken\PSI_Outputs\Test_Small_Run\Tract_Centroids_Test.shp"  list2 = []  #fclist = arcpy.ListFeatureClasses("*","Point") #for fc in fclist:     #list2.append(env.workspace + os.sep + raster)     #print 'Successfully created list2'  rows = arcpy.SearchCursor(featureClass) row = rows.next() while row:         print row.Sum_Count         row = rows.next()   count = len(list1)  x = 0 while x < count:     raster = list1[x]     name = raster.split("\\")[-1]     inParcelNumber = row.Sum_Count     outTimes = raster * inParcelNumber     outTimes.save(r"E:\Models_Ken\PSI_Outputs\Test_Small_Run_Output" + os.sep + name.split(".")[0] + "_minus.tif")     print 'Successfully created raster'     x += 1  del list1, row.Sum_Count


This script gives me the output as follows:
>> ================================ RESTART ================================
>>>
Successfully created list1
[u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_0_step1_minus.tif']
Successfully created list1
[u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_0_step1_minus.tif', u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_1_step1_minus.tif']
Successfully created list1
[u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_0_step1_minus.tif', u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_1_step1_minus.tif', u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_2_step1_minus.tif']
Successfully created list1
[u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_0_step1_minus.tif', u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_1_step1_minus.tif', u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_2_step1_minus.tif', u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_3_step1_minus.tif']
Successfully created list1
[u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_0_step1_minus.tif', u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_1_step1_minus.tif', u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_2_step1_minus.tif', u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_3_step1_minus.tif', u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_4_step1_minus.tif']
Successfully created list1
[u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_0_step1_minus.tif', u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_1_step1_minus.tif', u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_2_step1_minus.tif', u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_3_step1_minus.tif', u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_4_step1_minus.tif', u'E:\\Models_Ken\\PSI_Outputs\\Test_Small_Run\\PSI_Census_SourceCalculation_5_step1_minus.tif']
0
6
164
122
9
0

Traceback (most recent call last):
  File "E:\Models_Ken\PSI_Outputs\Scripts\PSI_Multiply.py", line 41, in <module>
    inParcelNumber = row.Sum_Count
AttributeError: 'NoneType' object has no attribute 'Sum_Count'
--------------------------------------------------------------------------------------------------------------------------------------------------------
As you can see, my list 1 is populated with the rasters I specified. The last set of numbers being 0,6,164,122,9,0 are the values that I am pulling out of the shapefiles Sum_Count field. These would be the values that I am looking to do my multiplication by, but instead of these values being stored in a list I am only able to print them out in this fashion.

Sorry if this is confusing what I am trying to do by how I am describing it here, if anyone has any thoughts or questions about what I am trying to do I would really appreciate the feedback.

Thanks,

Zach Ancona

Outcomes