Select to view content in your preferred language

String Index with Cursor in ArcPro

674
1
Jump to solution
04-23-2019 01:32 PM
JaredPilbeam2
MVP Regular Contributor

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.

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Jared,

Should the path be the full name of the JPG?  For example, \Projects\DOT_SignInventory\CH01\0110100_2018-04-27.JPG.

If that's the case, you can remove the var variable and use the following:

jpgs[fname[:7] = os.path.join(dirName,fname)

View solution in original post

1 Reply
JakeSkinner
Esri Esteemed Contributor

Hi Jared,

Should the path be the full name of the JPG?  For example, \Projects\DOT_SignInventory\CH01\0110100_2018-04-27.JPG.

If that's the case, you can remove the var variable and use the following:

jpgs[fname[:7] = os.path.join(dirName,fname)