consolidate two fields strings into one field

2968
5
Jump to solution
12-23-2015 09:02 AM
CCWeedcontrol
Occasional Contributor III

I have the following code, the code runs without errors but the problem i am have is that i need to populate the "StreetName" field with two other fields, "SiteStreet" & "StreetType". So the "StreetName" Field would update with "Galloway Rd". Currently only the "SiteStreet" populates the "StreetName", the "StreetType" doesn't get added to the "StreetName" Field. I thought i had this working previously but i am having a hard time figuring out why it won't populate. Any ideas on what's going on?

I have checked that the StreetType_1 field exists in the spatial join with the following

field = 'StreetType'
    values = [row[0] for row in arcpy.da.SearchCursor(sjpoints, (field))]
    uniqueValues = set(values)
    print(uniqueValues)

the print out reads.

set([u'Rd'])

# Import arcpy module
import arcpy
from arcpy import env


# Allow overwrite 
arcpy.env.overwriteOutput = True

# Script user input parameters
polygonLayer = "DSD.DBO.TaxParcels1" #Taxparcels
pointLayer = "TESTCCAP.DBO.CCAP1" #Point Layer
arcpy.env.workspace = r"Database Servers\DSD15_SQLEXPRESS.gds\TESTCCAP (VERSION:dbo.DEFAULT)" #arcpy.env.workspace = r"Database Servers\DSD15_SQLEXPRESS.gds\TEST (VERSION:dbo.DEFAULT)"
poly = "ACCOUNT_1" 
Pnt =  "Account"
sjpoints = "In_memory\sjpoints" 

parcelsCount = int(arcpy.GetCount_management(pointLayer).getOutput(0))     
    
dsc = arcpy.Describe(pointLayer) 

selection_set = dsc.FIDSet         
if len(selection_set) == 0:       
    print "There are no features selected"  
             
elif parcelsCount >= 1: 

    #Run the Spatial Join tool, using the defaults for the join operation and join type
    arcpy.SpatialJoin_analysis(pointLayer, polygonLayer, sjpoints)

    # define the field list from the spatial join
    sourceFieldsList = ['TARGET_FID', poly,'SiteAddress','SiteNum_1', 'SiteStreet_1','SiteNumSfx_1','Predir_1','SiteStreet_1', 'Postdir_1', 'SiteCity_1', 'SiteZIP_1', 'OwnerName_1','StreetType_1']    #,'StreetType_1'

    # define the field list to the original points
    updateFieldsList = ['OID@', Pnt,'SiteAddres', 'SiteNum', 'StreetName', 'SiteNumSfx','Predir','SiteStreet', 'Postdir', 'SiteCity', 'SiteZip', 'OwnerName','StreetType'] #, 'StreetType'

    field = 'StreetType'
    values = [row[0] for row in arcpy.da.SearchCursor(sjpoints, (field))]
    uniqueValues = set(values)
    print(uniqueValues)
            
    # Start an edit session. Must provide the workspace.    
    edit = arcpy.da.Editor(arcpy.env.workspace)    
  
    # Edit session is started without an undo/redo stack for versioned data    
    #  (for second argument, use False for unversioned data)    
    edit.startEditing(True)    
  
    # Start an edit operation    
    edit.startOperation()    
     
    manualFields =  ['FacltyType','GIS_STEW', 'StructType', 'Verified', 'Status', 'StructCat', 'APA_CODE','StreetName'] #,'StreetName'
           
    with arcpy.da.UpdateCursor(pointLayer, manualFields) as rows:       
        for row in rows:       
            row[0] = ("Single Family Home")
            row[1] = ("Canyon")
            row[2] = ("Primary, Private")           
            row[3] = ("Yes, GRM, TA")           
            row[4] = ("Active")           
            row[5] = ("Residential")           
            row[6] = ("1110")
            row[7] = (sourceFieldsList[4] + " " + sourceFieldsList[12])
            rows.updateRow(row)  
        del row       
        del rows 
    # populate the dictionary from the polygon
    valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sjpoints, sourceFieldsList)}    

    with arcpy.da.UpdateCursor(pointLayer, updateFieldsList) as updateRows:    
        for updateRow in updateRows:    
            keyValue = updateRow[0]
            if keyValue in valueDict:
                for n in range (1,len(sourceFieldsList)):      
                    updateRow = valueDict[keyValue][n-1]
                updateRows.updateRow(updateRow)
        del updateRows
        del updateRow

    #Update feature attributes to proper case
    fields1 = ['StreetName', 'SiteStreet','SiteAddres','OwnerName','StreetType']
    with arcpy.da.UpdateCursor(pointLayer, fields1) as cursor:
        for row in cursor:
            row[0] = row[0].title()
            row[1] = row[1].title()
            row[2] = row[2].title()
            row[3] = row[3].title()
            row[4] = row[4].title()
            cursor.updateRow(row)
            
    # Stop the edit operation.    
    edit.stopOperation()
    # Stop the edit session and save the changes
    edit.stopEditing(True)

arcpy.RefreshActiveView()
#arcpy.Delete_management(sjpoints)
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

This line:

row[7] = (sourceFieldsList[4] + " " + sourceFieldsList[12])

...refers to the 5th and 13th value of the list sourceFieldsList. Those values are strings (not cell values), literally: 'StreetSite_1' and 'StreetType_1'. So, when you concatenate these values, it should return 'StreetSite_1 StreetType_1', again, not the cell values.

I'll say again, add a print statement within the update cursor to confirm the values you are trying to concatenate.

View solution in original post

5 Replies
DarrenWiens2
MVP Honored Contributor

Help me out here - where are you trying and failing to concatentate these fields? Line 62? As has been suggested in your other threads, use print statements to confirm that the values you think should be there, are there.

You check that 'StreetType' is there, but you're referencing "StreetType_1" - perhaps check that that's there.

CCWeedcontrol
Occasional Contributor III

correct on line 62. my bad line 36 should be 'StreetType_1. After making the change and re-running the print out reads.

the print out reads.

set([u'Rd'])

0 Kudos
DarrenWiens2
MVP Honored Contributor

This line:

row[7] = (sourceFieldsList[4] + " " + sourceFieldsList[12])

...refers to the 5th and 13th value of the list sourceFieldsList. Those values are strings (not cell values), literally: 'StreetSite_1' and 'StreetType_1'. So, when you concatenate these values, it should return 'StreetSite_1 StreetType_1', again, not the cell values.

I'll say again, add a print statement within the update cursor to confirm the values you are trying to concatenate.

CCWeedcontrol
Occasional Contributor III

my apologies for not understand correctly.

I have been trying to put a print statement for the values but i am not having much success.

print

set([u'CT'])
SiteStreet_1 StreetType_1
0 Kudos
DanPatterson_Retired
MVP Emeritus

the point Darren is trying to make is that it is NOT returning the values from those field, since you haven't told it to do so...it is returning the field names as you have instructed...you need to get the values from those fields and concatenate them together.  So the print statement are working...they are showing you are doing it wrong