I have two tables (actually both are feature classes in filegeodatabase).
They pretty much identical except. one is in lat/lon. second one is projected.
both have field line_length and objectid.
Second one has values in field line_length.
how do I copy these values (based on objectid field match) from second table into the fist one.
The snippet of my code:
Part first. Create dictionary from second dataset. Copy values from second dataset into dictionary. value that I will be using for matching is goint to be key, the value I need to copy to my first dataset are going to be values. This part works ok.
my_dict = dict()
fc = r'H:\NetworkAnalyst\TopologyVer4\firetrials.gdb\ft_mb_afterIteration_lambert'
fields = ['OBJECTID', 'line_length']
with arcpy.da.SearchCursor(fc, fields) as cursor:
for row in cursor:
a_id = row[0]
b_len = row[1]
my_dict[a_id] = b_len
print "dictionary done"
Second part. Update field "line_length" in the first dataset with values from second dataset, which are now in dictionary. Match records by OBJECTID field.
rows = arcpy.UpdateCursor(r'H:\NetworkAnalyst\TopologyVer4\firetrials.gdb\ft_mb_afterIteration_lambert')
for key, value in my_dict:
match_value = key
update_value = value
for row in rows:
print match_value, update_value
if row.OBJECTID == match_value:
rows.setValue("line_lenght", update_value)
I am getting an error for line
for key, value in my_dict:
Error is " TypeError: 'int' object is not iterable "
Any ideas?
Best Answer
Pre-code:
What Vince was referring to as nested cursors:
Code:
A dictionary based solution (I use this most often):
Additional comments:
arcpy.da.UpdateCursor
, notarcpy.UpdateCursor
)Update: explaining some of the concepts in the dictionary alternative.
means we are taking the first value in the tuple (cursor returns a list of two-values tuple, such as
[(12,45.678),(13,78.654)]
, which represents theOBJECTID
, and make it a key of the dictionary; we map this key into a value ofAcres
(second element in the tuple).This is the same as writing:
The dictionary comprehensions are just faster and less code can be written and then read.
Another thing.
Here we assign the
AcresObtained
field (which is the second value in the tuple in the update cursor) to be a value that is found for thisOBJECTID
key in the dictionarysearch_feats
.Could be written as