Select to view content in your preferred language

UpdateCursor list indices must be intergers, not str

3192
3
Jump to solution
11-25-2015 09:42 AM
CCWeedcontrol
Frequent Contributor

currently have a code that i would like to update a certain field based off of two other fields but i am getting a error.

Error:TypeError: list indices must be integers, not str.

on line : row[7] = (sourceFieldsList['SiteStreet_1'] + " " + sourceFieldsList['StreetType_1'])

The only difference in the fields is that some have different lengths, the fields are 'String' fields so i am not sure what's going on other then the lengths. Do the fields have to be the same lengths?

Any help would be great, thanks!

# 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']    #

    # define the field list to the original points
    updateFieldsList = ["OID@", Pnt,"SiteAddres", 'SiteNum', 'StreetName', 'SiteNumSfx','Predir','SiteStreet', 'Postdir', 'SiteCity', 'SiteZip', 'OwnerName' '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'] #]
           
    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] = ("LM")
            row[5] = ("Residential")           
            row[6] = ("1110")
            row[7] = (sourceFieldsList['SiteStreet_1'] + " " + sourceFieldsList['StreetType_1'])
            rows.updateRow(row)
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Alum

I'm not exactly sure what you're trying to do with this code, but neither:

sourceFieldsList['SiteStreet_1'] # error

...nor...

sourceFieldsList[4] # the string 'SiteStreet_1'

...will provide you with the value of a cell in the column, 'SiteStreet_1'. But, if you just want the string, 'SiteStreet_1', then reference it in position 4, as above.

View solution in original post

3 Replies
DanPatterson_Retired
MVP Emeritus

You are slicing by text instead of a number

sourceFieldsList['SiteStreet_1']  # change 'SiteStreet_1' to the index number within that list

FreddieGibson
Honored Contributor

What are you trying to accomplish with the following logic?

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']
sourceFieldsList['SiteStreet_1'] + " " + sourceFieldsList['StreetType_1'] 

You won't be able to slice a list in python with a string. Are you trying to get the index of the word in the list?

DarrenWiens2
MVP Alum

I'm not exactly sure what you're trying to do with this code, but neither:

sourceFieldsList['SiteStreet_1'] # error

...nor...

sourceFieldsList[4] # the string 'SiteStreet_1'

...will provide you with the value of a cell in the column, 'SiteStreet_1'. But, if you just want the string, 'SiteStreet_1', then reference it in position 4, as above.