Newbie for loop question

783
14
11-17-2011 10:52 AM
AndrewMosley
New Contributor
Hi there,

I am trying to move an ID number from the attribute table of a census block file to the attribute table of a roads file. The blocks are all separate feature classes in a gdb and the roads file is one single layer. So far I've decided that the simplest way to do this may be to select all roads with their center in each block, get the STFID from the block attribute table, then assign that STFID to the roads selected in the roads attribute table. The code I've got so far will select all roads at the same time, instead of running through the process one at a time. Current syntax as follows:

>>> import arcpy
>>> #Set geoprocessing environment
>>> arcpy.env.workspace = ("H:\Dr. Hines\New File Geodatabase.gdb")
>>> arcpy.env.overwriteOutput = True
>>> #Create list of all blocks
>>> fcList = arcpy.ListFeatureClasses("*blk00")
>>> #Create loop
>>> for fc in fcList:
...      blk = fcList
...      arcpy.SelectLayerByLocation_management("Analysis Roads","HAVE_THEIR_CENTER_IN")
...      for fc in fcList:
...         rows = arcpy.InsertCursor(blk)
...         row = rows.newRow()
...         row.STFID = ()
...         fieldVal = row.getValue("STFID")
...         arcpy.CalculateField_management("Analysis Roads","STFID","fieldVal")
...
Runtime error <type 'exceptions.RuntimeError'>: ERROR 999999: Error executing function.

any help is greatly appreciated!
Tags (2)
0 Kudos
14 Replies
AndrewMosley
New Contributor
Using the above code returns this error:
Runtime error <type 'exceptions.AttributeError'>: 'unicode' object has no attribute 'STFID'

I am trying to move the STFID value from the blocks layers to the roads layer.
STFID of block1------\
STFID of block2------    \ STFID in roads layer (all null/empty values)
STFID of block3------    /
STFID of block4------/

I'm not sure why it says the unicode value has no attribute STFID, as all layers in this do have an STFID layer which are all string of the same length.

In the end though, I want the selected roads to be assigned the STFID value of the block they were selected by.
0 Kudos
MathewCoyle
Frequent Contributor
I suspected that might happen, your fc variable is a feature class and has no attribute STFID. It is simply a unicode reference to the feature class in the list.

Does each block feature class have a single unique STFID value? You will have to retrieve that value somehow, either assigned manually, read from a field the same way you do with the roads, or some other way.

If you don't understand this concept you should try some more simplistic operations to get a grasp of basic arcpy functionality. Esri has some good free online training courses to get you started.
Try working through this, it gives a great intro to arcpy and python in general.
http://training.esri.com/gateway/index.cfm?fa=catalog.webCourseDetail&courseid=1868
0 Kudos
AndrewMosley
New Contributor
Yes, each block has a STFID value. However they are all the same with each block's attribute table, as this is the value I want to assign to any roads within this block's geometry.
0 Kudos
MathewCoyle
Frequent Contributor
Have you tried looking at the intersect tool?
0 Kudos
AndrewMosley
New Contributor
The roads pass through multiple blocks so the intersect tool will return too many results (more than 1).
I'm wondering if something like this would work:

for fc in fclist:
    arcpy.selectLayerByLocation_management(highway,"Have_their_center_in", fc)
    arcpy.GetValue(STFID from the current fc table)
    arcpy.UpdateCursor(STFID for selected roads in roads table)


though im not sure on how the coding would look exactly.
0 Kudos