import arcpy import os import string import subprocess arcpy.env.workspace = r"c:\Users\matt\Desktop\Ditch\Database\Centerline.mdb" assetFeatureClass = "[Assets]" assetrows = arcpy.UpdateCursor(assetFeatureClass,"[Processed] <> 'Y'","","","") # for each row in the result set for row in assetrows: print row.getValue("Ditch_ID") #create asset #Insert code to call out ArcObjects utility... centerline = "[Pipeline_Centerline]" #update asset record attributes from asset table rows = arcpy.UpdateCursor(centerline) for row in rows: row.Ditch_ID = assetFeatureClass.Ditch_ID rows.updateRow(row)
Runtime error Traceback (most recent call last): File "<string>", line 24, in <module> AttributeError: 'str' object has no attribute 'Ditch_ID'
Solved! Go to Solution.
assetFeatureClass.Ditch_ID
for rowC in rows:
>>> listA = ['FID', 'Shape', 'COVER', 'RECNO'] >>> listB = ['OID', 'Geometry', 'String'] >>> for a, b in zip( listA, listB): ... print "a: ", a ," b: ", b ... a: FID b: OID a: Shape b: Geometry a: COVER b: String
assetRows = arcpy.UpdateCursor(assetFeatureClass,"[Processed] <> 'Y'","","","") centerRows = arcpy.UpdateCursor(centerline) for assetrow, rowCenter in zip(assetRows, centerRows): centerRow.Ditch_ID = assetRow.Ditch_ID #etc...
assetFeatureClass.Ditch_ID
for rowC in rows:
>>> listA = ['FID', 'Shape', 'COVER', 'RECNO'] >>> listB = ['OID', 'Geometry', 'String'] >>> for a, b in zip( listA, listB): ... print "a: ", a ," b: ", b ... a: FID b: OID a: Shape b: Geometry a: COVER b: String
assetRows = arcpy.UpdateCursor(assetFeatureClass,"[Processed] <> 'Y'","","","") centerRows = arcpy.UpdateCursor(centerline) for assetrow, rowCenter in zip(assetRows, centerRows): centerRow.Ditch_ID = assetRow.Ditch_ID #etc...
centerrow.Ditch_ID = assetrow.Ditch_ID centerrow.Event_ID = assetrow.Event_ID centerrow.Comments = assetrow.Comments centerrow.Diameter = assetrow.Diameter
import arcpy fc = 'C:/data.shp' fields = [ 'Event', 'Comments', 'Diameter' ] cursor = arcpy.da.SearchCursor( fc , fields ) for row in cursor: for attr in row: print attr
cursor = arcpy.da.SearchCursor(centerline, fields) for row in cursor: for attr in row: row.cursor = assetrow.? #what should this line look like? #centerrow.Ditch_ID = assetrow.Ditch_ID #centerrow.Event_ID = assetrow.Event_ID #centerrow.Comments = assetrow.Comments #centerrow.Diameter = assetrow.Diameter #print centerrow.getValue("Ditch_ID") #centerRows.updateRow(centerrow)
assetRows = arcpy.da.SearchCursor( fc1 , fields ) centerRows = arcpy.da.UpdateCursor( fc2 , fields ) for assetRow, centerRow in zip(assetRows, centerRows): for assetAttr, centerAttr in zip(assetRow, centerRow): centerAttr = assetAttr centerRows.updateRow(centerRow)
readFieldsList = ["OBJECTID", "RID", "MEAS", "X_COORD", "Y_COORD", "X_Y_LINK", "STNAME", "STNAMES", "X_Y_ROUTE"] valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(readFC, readFieldsList)} updateFieldsList = ["FROM_OBJECTID","FROM_ROUTE_NAME","FROM_MEASURE","FROM_X_COORDINATE","FROM_Y_COORDINATE","FROM_X_Y_LINK","FROM_STREET_NAME","FROM_CROSS_STREETS","FROM_X_Y_ROUTE_NAME"] with arcpy.da.UpdateCursor(updateFC, updateFieldsList) as updateRows: for updateRow in updateRows: ObjectIDVal = updateRow[0] if ObjectIDVal in valueDict: updateRow[1] = valueDict[ObjectIDVal][0] updateRow[2] = valueDict[ObjectIDVal][1] updateRow[3] = valueDict[ObjectIDVal][2] updateRow[4] = valueDict[ObjectIDVal][3] updateRow[5] = valueDict[ObjectIDVal][4] updateRow[6] = valueDict[ObjectIDVal][5] updateRow[7] = valueDict[ObjectIDVal][6] updateRow[8] = valueDict[ObjectIDVal][7] updateRow[9] = valueDict[ObjectIDVal][8] updateRows.updateRow(updateRow)
Hi Richard,
I am relatively new to Python and search cursors. Can you explain what the value dictionary does?
Thanks in advance!
Judson
First read my Blog post called Turbo Charging Data Manipulation with Python Cursors and Dictionaries. It covers this subject in much more depth with several examples of how to apply the technique and it provides code that is more standardized which should be easier to customize to your needs.
As far as a Python dictionary it works like a real world dictionary. If you look up a word in a dictionary (a dictionary key) you will find all kinds of information related to that word (the value associated with the key). Python dictionaries offer is an extremely fast way to directly lookup related information through meaningful key values, as opposed to lists which only support a lookup using generally meaningless index number values or traversing the list in a loop. Python dictionary keys are not limited to words and can be nearly anything that is an immutable value. Tuples can be keys and can be used to create composite value keys (i.e., multi-field keys like the separate components of an address). Lists and Dictionaries are mutable and cannot be used as keys, but they can be values associated with a lookup key.
Thanks for the link to your blog post! That is exactly what I was looking for. I appreciate you taking the time to explain this.