AnsweredAssumed Answered

String Index with Cursor in ArcPro

Question asked by jpilbeam Champion on Apr 23, 2019
Latest reply on Apr 23, 2019 by jskinner-esristaff

Hello,

The intention of this script is to update the 'Photo' field of a feature class table with the full path of a JPG based on the 'Post_Number' field. The problem is only the first seven digits of the JPG matches the unique ID in the 'Post_Number' field. The rest of the file name is a date. I've placed a string index number variable in a for loop that gets the JPG by the first seven digits.  A dictionary is then built off the existing paths without the date. The cursor is then supposed to update the 'Photo' field. The script runs without error but the Photo field isn't updated?? 

 

I'm thinking the 'var' variable is in the wrong place. 

 

'''Python 3.6.6. The purpose of this script is to update the 'Photo' field in the fc
with the full path of a JPG.
'''

import arcpy, os

ws = r'path\to\Projects\DOT_SignInventory\pics'
jpgs = {} # empty dictionary

for dirName, subdirList, fileList in os.walk(ws):
    for fname in fileList:
        var = fname[:7] # get first seven digits of file name
        jpgs[var[:-4]] = os.path.join(dirName,var)

fc = r'path\to\Projects\DOT_SignInventory\Data2.gdb\WCDOT_Signs'
fields = ['OID@', 'Post_Number', 'Photo'] # fields of FC

with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        try:
            row[2] = jpgs[row[1]]
            cursor.updateRow(row)
        except KeyError:
            print("No photo for: {}".format(row[1]))

del cursor

 

Here's how the JPG looks in the directory:

 

 

Here's how I want the Photo field to be updated. Right now it's empty. Both fields are text fields.

Outcomes