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

701
4
Jump to solution
04-26-2013 01:53 PM
ZachAncona
New Contributor
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     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
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MikeHunter
Occasional Contributor
I'm not really clear on what you are trying to do, but you seem to want a list of the values in the Sum_Count field.  All you have to do is replace your code:
rows = arcpy.SearchCursor(featureClass) row = rows.next() while row:         print row.Sum_Count         row = rows.next()


with:

rows = arcpy.SearchCursor(featureClass) value_lst = [row.Sum_Count for row in rows]


Now you'll have a list of the values in the Sum_Count field, and you can do what you want with them later in the code.

The error you were getting was raised when you tried to reference the row object after the last next() call, which always returns None when the end-of-file is encountered.  Use a value from the value_lst instead, and that error will go away.

good luck,
Mike

View solution in original post

0 Kudos
4 Replies
MikeHunter
Occasional Contributor
I'm not really clear on what you are trying to do, but you seem to want a list of the values in the Sum_Count field.  All you have to do is replace your code:
rows = arcpy.SearchCursor(featureClass) row = rows.next() while row:         print row.Sum_Count         row = rows.next()


with:

rows = arcpy.SearchCursor(featureClass) value_lst = [row.Sum_Count for row in rows]


Now you'll have a list of the values in the Sum_Count field, and you can do what you want with them later in the code.

The error you were getting was raised when you tried to reference the row object after the last next() call, which always returns None when the end-of-file is encountered.  Use a value from the value_lst instead, and that error will go away.

good luck,
Mike
0 Kudos
ZachAncona
New Contributor
Hey Mike,

Thanks I will give this a try when I get back to the office next week. Thank you for the reply!

-Zach
0 Kudos
ZachAncona
New Contributor
Worked like a charm! If you are interested in what I was doing with the lists I have attached the working code below.

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

arcpy.CheckOutExtension("Spatial")

env.workspace = r"E:\Models_Ken\PSI_Outputs\Subtraction_Output_Tract"

list1 = []

lstRasters = arcpy.ListRasters("*")
for raster in lstRasters:
    list1.append(env.workspace + os.sep + raster)
    print 'Successfully created list1'
    print list1

featureClass = r"E:\Models_Ken\PSI_Outputs\tract_centroids.shp"


rows = arcpy.SearchCursor(featureClass)
value_lst = [row.Sum_Count for row in rows]

print value_lst

count = len(list1)

x = 0
while x < count:
    raster = list1
    name = raster.split("\\")[-1]
    inParcelNumber = value_lst
    outTimes = Times(raster, inParcelNumber)
    outTimes.save(r"E:\Models_Ken\PSI_Outputs\Multiplication_Output_Tract" + os.sep + name.split(".")[0] + "_multi.tif")
    print 'Successfully created raster'
    x += 1

del list1, value_lst, count


Thanks again for your help.

-Zach
0 Kudos
MikeHunter
Occasional Contributor
Yes, I see now.  Glad you got it working!

Mike
0 Kudos