I am writing a python script that reads a csv file that contain the OBJECTID and a CompKey. I am using a csv dictionary to store the values in. Once they are read in, I want to use an updatecursor to update the compkey based on a matching OBJECTID, but I can't get the update cursor to find any matching values. My update cursor looks like this:
with arcpy.da.UpdateCursor(fc, fc_fields) as cursor: for row in cursor: OBJECTID = (row[0]) print OBJECTID # this loop is to assign each field in the dictionary to the cooresponding field in the fc_fields list if csvdict.has_key(OBJECTID): print('found OBJECTID for {}'.format(OBJECTID)) row[1] = csvdict[OBJECTID][0] cursor.updateRow(row) print cursor else: print ('no matching OBJECTID') When I run the entire script all I get is the no matching objectid. The above script worked when comparing to a field other than OBJECTID. When I change OBJECTID to UNITID, it works, the problem is UNITID is not a unique value, so I am getting duplicates. Any help would be greatly appreciated.
I am wondering if the OID is coming in from the CSV as text and not an integer.
If OID from CSV is coming as text, than you would not get any matches, so it would be good to verify.
Since the OID from the CSV is text, try:
if csvdict.has_key(str(myOID)): # convert myOID to string
print('found myOID for {}'.format(myOID))
row[1] = csvdict[str(myOID)][0] # convert myOID to string
cursor.updateRow(row)
#...
This section starts at line 69 in Joe's code, and use the correct indentation for this line. Also, the values in csvdict are also text, so you need to convert them to the correct type.
If your OIDs start at 321972, you may wish to use a where clause so you are not checking unnecessary rows.
I finally got it working, as it turned out it was the csv file being read in as text and not an integer. I also had to read my compkey in as an integer also. The code that changed was :
#Read the CSV File and store in csvdict dictionary with open(CompKey, 'r') as f: reader = csv.reader(f) #skip header _ = next(f) for row in reader: key = [int(row[0])] #THIS IS THE LINE THAT CHANGED. ######################## csvdict[int(float(row[0]))] = [int(float(row[1]))] print (csvdict) print "That was the dictionary"
Thanks to all that helped.
What a difference a day makes....
Yes, you are so right! Thanks again for you help.