2

I have a script that is used to update a line feature class with the start and end elevations based off of a shared ID number with a point feature class using list comprehension in arcpy. However I have an issue where the ID field from the points is a long integer and the ID field in the lines is a string field. Can I write something in my script to read in the integer values from the point layer as strings so that the list comprehension will work? Here is my current script that runs between matching integer field types, but not between mismatched field types:

from time import strftime  
print "Start script: " + strftime("%Y-%m-%d %H:%M:%S")  
import arcpy

#Transfer of Multiple Field Values between Feature Classes where there is a 1:1 Match between Field Sets
#keyValue fields must be identical field types (string, integer, etc.)

sourceFC = "Boxes"
strBoxID = str('Box_ID') #read the 'Box_ID' field as a string due to mismatching fieldtypes between boxes and lines

sourceFieldsList = [strBoxID, 'Elevation', strBoxID, 'Elevation']  # Your Source fields  

# Use list comprehension to build a dictionary from a da SearchCursor  
valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, sourceFieldsList)}  

updateFC = "LinesTest"  

updateFieldsList = ['FBOX', 'F_ELEV', 'TBOX', 'T_ELEV']  # your updateFC fields

with arcpy.da.UpdateCursor(updateFC, updateFieldsList) as updateRows:  
    for updateRow in updateRows:  
        # store the Join value of the row being updated in a keyValue variable  
        keyValue = updateRow[0]  
        # verify that the keyValue is in the Dictionary  
        if keyValue in valueDict:  
            # transfer the values stored under the keyValue from the dictionary to the updated fields.  
            for n in range (1,len(sourceFieldsList)):  
                updateRow[n] = valueDict[keyValue][n-1]  
            updateRows.updateRow(updateRow)  

del valueDict  

print "Finished script: " + strftime("%Y-%m-%d %H:%M:%S")  
  • 3
    Not as it's currently written. You'll need to cast those values to string after they come out of the search cursor and then add them to your dict. You would cast the values like str(r[0]):(r[1:]) or str(r[0]):(str(r[1:])). You get the values from the rows in your search cursor. – jbalk Jan 30 at 20:34
  • Thanks that helped get the integer values copied into the string field! – cbunn Jan 30 at 20:57
3

Following the comment by @jbalk I cast the values from the integer to string values when they get added to the dictionary. I also updated my code so it can populate the elevation fields for both the start and end points correctly and also populate a field that combines the Box ID's of both the beginning and end points:

from time import strftime  
print "Start script: " + strftime("%Y-%m-%d %H:%M:%S")  
import arcpy

#Transfer of Multiple Field Values between Feature Classes where there is a 1:1 Match between Field Sets

sourceFC = "Boxes"

sourceFieldsList = ['Box_ID', 'Elevation']  # Your Source fields  

# Use list comprehension to build a dictionary from a da SearchCursor  
valueDict = {str(r[0]):(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, sourceFieldsList)}  

updateFC = "Lines"  

updateFieldsListFrom = ['FBOXID', 'F_ELEV']  # your updateFC fields

with arcpy.da.UpdateCursor(updateFC, updateFieldsListFrom) as updateRows:  
    for updateRow in updateRows:  
        # store the Join value of the row being updated in a keyValue variable  
        keyValue = updateRow[0]  
        # verify that the keyValue is in the Dictionary  
        if keyValue in valueDict:  
            # transfer the values stored under the keyValue from the dictionary to the updated fields.  
            for n in range (1,len(sourceFieldsList)):  
                updateRow[n] = valueDict[keyValue][n-1]  
            updateRows.updateRow(updateRow)  


updateFieldsListTo = ['TBOXID', 'T_ELEV']  # your updateFC fields

with arcpy.da.UpdateCursor(updateFC, updateFieldsListTo) as updateRows:  
    for updateRow in updateRows:  
        # store the Join value of the row being updated in a keyValue variable  
        keyValue = updateRow[0]  
        # verify that the keyValue is in the Dictionary  
        if keyValue in valueDict:  
            # transfer the values stored under the keyValue from the dictionary to the updated fields.  
            for n in range (1,len(sourceFieldsList)):  
                updateRow[n] = valueDict[keyValue][n-1]  
            updateRows.updateRow(updateRow)  

del valueDict
#update "FROM_TO" field
arcpy.CalculateField_management("Lines","FROM_TO",'!FBOXID!+"-"+ !TBOXID!',"PYTHON")

print "Finished script: " + strftime("%Y-%m-%d %H:%M:%S")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.