The logic behind what you're doing is somewhat unclear, but it sounds like you're trying to sort a bunch of records on a few different fields and then go through them one at a time trying to compare each record with some previous record based on the STID
field and if neighboring records don't have the same STID
field, then you move to the next until they match.
I think the trouble you're getting into, and this is a shot in the dark, is that search cursors don't support looping backwards. So if you're looking for STID-x
and you iterate through your search cursor rows looking for STID-y == STID-x
, you may end up at the end of your cursor object before you've done your complete analysis on each record. You can add some print
statements to see what's going on for each iteration in your search cursor to help pinpoint where your script is going wrong.
Also, based on the fields and data you supplied above, it looks like you want to be comparing the OBID
field to the STID
field. If that's the case you need to re-look at your sorting and your getValue
statements.
EDIT:
Ok, I got this to work. It looks like your problem was caused because you don't have a 'proper' Object ID for your rows. Not quite sure why this works, but I added a field called FID
in Access and set it's type to 'AutoNumber' which stores a unique value for each record (even though your OBID field is unique already). Maybe someone else can enlighten us both. After doing that, I added logic to scrape your records from the 'roads' table into a new table called 'output' which is either created from scratch or cleared of any pre-existing data each time you run this script.
import arcpy
ws = 'C:/temp/test.mdb'
arcpy.env.workspace = ws
arcpy.env.overwriteOutput = True
rows = arcpy.SearchCursor('road','','','STID;FL;TL','STID A; FL A; TL A')
#-- create new table to hold results using 'road' table as template
if arcpy.Exists('output'):
arcpy.DeleteRows_management('output') #-- clear contents of output table
else:
arcpy.CreateTable_management(ws,'output','road')
arcpy.AddField_management('output','originalFID','LONG')
irows = arcpy.InsertCursor('output') #-- create insert cursor
first = True
for row in rows:
if first:
first = False
else:
fid = row.FID
stid = row.STID
fl = row.FL
tl = row.TL
if stid == prev_stid and fl <= prev_tl:
#-- add previous record to new table
irow = irows.newRow()
irow.originalFID = prev_fid
irow.STID = prev_stid
irow.TL = prev_tl
irow.FL = prev_fl
irows.insertRow(irow)
del irow
#-- now let's add the current record
irow = irows.newRow()
irow.originalFID = fid
irow.STID = stid
irow.TL = tl
irow.FL = fl
irows.insertRow(irow)
del irow
print stid,fl
#-- let's store all information from this row
prev_fid = row.FID
prev_stid = row.STID
prev_tl = row.TL
prev_fl = row.FL
del rows,irows
Since the geodatabase already exist, you could have used X-Ray for ArcCatalog add-in which was Developper by Esri, in conjunction with Vertex3. It allows you, amongst other things, to export the schema to spreadsheet, perform changes in the spreadsheet and import it back into a geodatabse.
X-Ray for ArcCatalog is available for 9.2 and 9.3 in ArcScript, and 10.x in ArcGIS Online.
Best Answer
Assuming you have the pyodbc library installed (there's an installer for Python 2.5 in their Downloads section) it's a simple matter of looping through each row of the Excel sheet. Something like:
If you don't want to use pyodbc, it should be easy enough to substitute in a csv reader or otherwise instead of the connection to the .xls. Good luck!