Select to view content in your preferred language

Iterate through a field?

3159
6
Jump to solution
02-07-2012 11:49 AM
LindaVasil
Occasional Contributor
Hi all,

I've written code that allows me to list adjacent neighbors to a new field.  I did this to pre-process a land use file because this task usually takes a long time to run.  What I would like to do with the new processed file that has an added field called "Neighbors" is to pull out each of the items (FID values) in the field, get the GRID_CODE associated with each FID and apply some rules to each point.  When I run the code, I get an error that TypeError: 'Field' object is not iterable

Does anyone have any suggestions of how to work around this?  I pulled out the Select by Feature and Select by Location code out of my main cellular automata processing code because it takes hours for the select by location command to run.  I only want to find the neighbors once, then update land use codes if conditions are right.  Do I have to create a new field for each neighbor?  Here's the code:

import sys, string, os, arcpy  arcpy.CheckOutExtension("Spatial")  ifc = sys.argv[1]  ily = "Input Layer"  desc = arcpy.Describe(ifc) arcpy.MakeFeatureLayer_management(ifc, ily, "", "", "") oid = desc.OIDFieldName  uc = arcpy.UpdateCursor(ifc) line = uc.next()  fields = arcpy.ListFields(ifc) for line in uc:     ci = line.getValue(oid)     arcpy.AddMessage("The current FID value is " + str(ci))     fi = line.getValue("GRID_CODE")     arcpy.AddMessage("The current GridCode value is " + str(fi))     OIDList = []  #Find the Neighbors field and just print the list for each point for now     for field in fields:         if field.name == "Neighbors":             for items in field:                 print items  del line del uc
 

Thanks for any help!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
LindaVasil
Occasional Contributor
hi Linda,

as the error states, you can´t iterate the field itself. you have to get its content into a list first, then you can iterate over each item.

#Find the Neighbors field and just print the list of ids for each point for now     nbList = [int(x) for x in line.Neighbors.split(";")]  # this should split all the ids of the current item(i.e. row) into a list, and convert the ids from string to integer     for nb in nbList:         print nb        # print each neighbor id 


hope this helps!


Thanks very much!!

View solution in original post

0 Kudos
6 Replies
MathewCoyle
Honored Contributor
I'm a little confused as to what you are trying to accomplish exactly. Are you trying to access the geometry component of the row? Or just modify the attribute values?

This link should give you all you need to access the geometry of the row features.
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Reading_geometries/002z0000001t000000/

If you just want attributes of the row, this will get you their values
rows = arcpy.UpdateCursor(layer)
for row in rows:
    for field in fields:
        print row.getValue(field.name)
0 Kudos
LindaVasil
Occasional Contributor
Sorry I wasn't too clear.  The main goal I'm trying to accomplish is cellular automata on a land use file.  I converted a raster to a point file, selected the adjacent neighbors and then I put a conditional statement in the cellular automata code that the cursor point will update to a different land use when the conditions are met.  I was using select by location to find the adjacent neighbor, but it was making the processing time into the days.  Since I run this land use simulation over and over again, I wanted to shorten the processing time.  I did this by first writing a code that selects the adjacent neighbors and putting the selection in a new field.  So I have a list of neighbors within one field.  This is the pre-processing code that takes hours to run...but that's ok because I only need it to run one time.  Then I'm trying to write another code that looks specifically at the list within the neighbors field and gives me the grid code for each neighbor.  That's where I'm running into problems because fields apparently cannot be iterated.  I will try playing around with your snippet, but I'm trying to find a list within a field not the whole row.  Maybe I can modify it, but if you have any suggestions after the further clarifications I would appreciate it!

Thanks!
0 Kudos
MathewCoyle
Honored Contributor
Alright I think I understand now. How is your list of neighbours stored in the field? String of IDs? If you could post an example of the field you are trying to access that may help.
0 Kudos
LindaVasil
Occasional Contributor
Alright I think I understand now. How is your list of neighbours stored in the field? String of IDs? If you could post an example of the field you are trying to access that may help.


The list of neighbors is stored as a string with a semicolon between FIDs.  I've attached a snapshot of the attribute table.

[ATTACH=CONFIG]11994[/ATTACH]

I tried the following using cursors

uc = arcpy.UpdateCursor(ifc)
line = uc.next()

fields = arcpy.ListFields(ifc)
for line in uc:
    ci = line.getValue(oid)
    arcpy.AddMessage("The current FID value is " + str(ci))
    fi = line.getValue("GRID_CODE")
    arcpy.AddMessage("The current GridCode value is " + str(fi))
    OIDList = []

#Find the Neighbors field and just print the list of ids for each point for now
    for field in fields:
        if field.name == "Neighbors":
            for item in field:
                line.getValue(item)
                print item

del line
del uc


I get this error when I run the code:

Traceback (most recent call last):
  File "C:\Python26\ArcGIS10.0\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Users\Flash\Documents\School\CA\ListFields.py", line 27, in <module>
    for item in field:
TypeError: 'Field' object is not iterable

I appreciate any help.  I've almost completed this using strictly python, but I would rather do it in ArcGIS to eliminate constantly loading up text files.

Thanks!
0 Kudos
RaphaelR
Deactivated User
hi Linda,

as the error states, you can´t iterate the field itself. you have to get its content into a list first, then you can iterate over each item.

#Find the Neighbors field and just print the list of ids for each point for now
    nbList = [int(x) for x in line.Neighbors.split(";")]  # this should split all the ids of the current item(i.e. row) into a list, and convert the ids from string to integer
    for nb in nbList:
        print nb        # print each neighbor id



hope this helps!
0 Kudos
LindaVasil
Occasional Contributor
hi Linda,

as the error states, you can´t iterate the field itself. you have to get its content into a list first, then you can iterate over each item.

#Find the Neighbors field and just print the list of ids for each point for now     nbList = [int(x) for x in line.Neighbors.split(";")]  # this should split all the ids of the current item(i.e. row) into a list, and convert the ids from string to integer     for nb in nbList:         print nb        # print each neighbor id 


hope this helps!


Thanks very much!!
0 Kudos