Invalid column value with arcpy.da.UpdateCursor

7523
20
Jump to solution
12-18-2015 10:00 AM
CCWeedcontrol
Occasional Contributor III

I have the following code and i am getting an error on line 76 with the arcpy.da.UpdateCursor that update feature attributes to proper case . I am unsure why. I would really appreciate some help please?

 

Thanks.

 

# Import arcpy module
import arcpy
from arcpy import env


# Allow overwrite 
arcpy.env.overwriteOutput = True

# Script user input parameters
polygonLayer = "TaxParcels1" #Taxparcels
pointLayer = "CCAP8" #Point Layer
arcpy.env.workspace = r"Database Servers\15_SQLEXPRESS.gds\CCAPTEST (VERSION:dbo.DEFAULT)" #arcpy.env.workspace = r"Database Servers\15_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'
    
    # 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"]#,'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
DanPatterson_Retired
MVP Emeritus

it doesn't get to print does it... which means that there is something before that... why don't you litter your code with print statements until you can figure out where you are going wrong.  Firstly, the error you are getting suggests there is a piece of garbage in the field, the field is incorrect or something.  Have you examined the field manually rather than relying on code?

View solution in original post

20 Replies
MichaelVolz
Esteemed Contributor

Maybe include a counter and use 1 line of code with counter for updating the row

cnt = 0

with arcpy.da.UpdateCursor(pointLayer, fields1) as cursor: 

     for row in cursor:

          row[cnt] = row[cnt].title() 

          cursor.updateRow(row)

           cnt += 1

Just a suggestion as I have not tested this.

0 Kudos
BlakeTerhune
MVP Regular Contributor

You can actually condense this a little by using enumeration instead of a separate cnt variable.

Re: Enumeration of a cursor

with arcpy.da.UpdateCursor(pointLayer, fields1) as u_cursor:
    for cnt, row in enumerate(u_cursor, start=0):
        # Process row and cnt; no need to update cnt variable.

However, I don't get what this is supposed to be doing. It doesn't make sense to me.

row[cnt] = row[cnt].title()
DarrenWiens2
MVP Honored Contributor

What exactly is the error message?

0 Kudos
CCWeedcontrol
Occasional Contributor III

oh ya that would be helpful, my bad

Error

Runtime error

Traceback (most recent call last):

  File "<string>", line 76, in <module>

RuntimeError: Invalid column value [StreetType]

After i get the error i also get ArcMap Drawing Errors:

CCAPTEST.DBO.CCAP8:  Invalid column value [StreetType]

0 Kudos
DarrenWiens2
MVP Honored Contributor

What data type is the "StreetType" field? And, if you add 'print row[4]' before attempting to change the value, what is the suspicious value just before throwing the error?

CCWeedcontrol
Occasional Contributor III

It is string(length 4),

I have tried adding the print but i am getting this error at

for row in cursor:

error RuntimeError: Invalid column value [StreetType]

 #Update feature attributes to proper case
    fields1 = ["StreetName", "SiteStreet","SiteAddres","OwnerName","StreetType"]#,'StreetType'
    with arcpy.da.UpdateCursor(pointLayer, fields1) as cursor:
        for row in cursor:
            print row[4]
            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)

I also tried adding the arcpy.da.SearchCursor before the arcpy.da.UpdateCursor and was given the same error.

# 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 = "CCAPTEST.DBO.CCAP8" #Point Layer
arcpy.env.workspace = r"Database Servers\DSD15_SQLEXPRESS.gds\CCAPTEST (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'
    
    # 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

    fields1a = ["StreetName", "SiteStreet","SiteAddres","OwnerName","StreetType"]#,'StreetType'
    with arcpy.da.SearchCursor(pointLayer, fields1a) as cursor:
        for row in cursor:
            print row[4]
        

    # Stop the edit operation.    
    #edit.stopOperation()
    # Stop the edit session and save the changes
    edit.stopEditing(True)

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

Runtime error

Traceback (most recent call last):

  File "<string>", line 82, in <module>

RuntimeError: Invalid column value [StreetType]

Am i adding the 'print' correctly?

0 Kudos
DanPatterson_Retired
MVP Emeritus

it doesn't get to print does it... which means that there is something before that... why don't you litter your code with print statements until you can figure out where you are going wrong.  Firstly, the error you are getting suggests there is a piece of garbage in the field, the field is incorrect or something.  Have you examined the field manually rather than relying on code?

CCWeedcontrol
Occasional Contributor III

Thanks for the replay, i have looked at the through the filed and they are populated with "Ave", "Rd" etc... there are blank/nulls attributes as well. I do see "NULL" and ' ' in some of the attributes. If i replace all the "Null" with " " the code doesn't populate any fields but if i replace all the ' ' with "NULL" the field "StreetName" gets populated but only with the SiteStreet_1. So i am thinking this row[7] = (updateFieldsList[4] + " " + updateFieldsList[12]) is the problem, some how it's not concatenating both fields together.

I have been trying to incorporate the print statements but have been unsuccessful.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Why are you commenting out the stopOperation call?  According to the ArcPy Data Access Editor documentation (red emphasis added):

Note:  Deviating from this pattern can cause unexpected results. For example, stopEditing should not be called while an edit operation is in progress. Instead, abort the edit operation and stop the edit session.