2

I want to populate a field, named FnotExist, with values from a dictionary. I want to check if the key of the dictionary matches the row value in field FI_PATH and if it does populate the corresponding row in FnotExist. The keys in the dictionary should match row values in FI_PATH because keys were created from this field. The code below is giving me the error:

TypeError: value #1 - unsupported type: list

when it reaches the line cursor.updateRow(row)

import arcpy

arcpy.env.workspace = r"C:\TestFolder"

dbf = "ProcessControlTable"

dbf1 = "APExistOnlyEdit.dbf"

myfield = "FI_PATH"


filepathlist2 =[row[0] for row in arcpy.da.SearchCursor(dbf1,myfield)]
#print(filepathlist2)

#Dictionary one holds field names that are within shapefile
d1 = {}

for thisFile in filepathlist2:
    ContainedFieldNames = [f.name.upper() for f in arcpy.ListFields(thisFile)]
    d1[thisFile] = ContainedFieldNames

print(d1)

#Dictionary two holds field names that are thought to be in shapefile
d2 = {}

with arcpy.da.SearchCursor(dbf1,[myfield,'AP_FIPS','AP_BLDGCOM','AP_TYPE','AP_STATUS','AP_ID','AP_HOUSENU','AP_HALFADD','AP_PREDIR','AP_PRETYPE','AP_STNAME','AP_SUFTYPE','AP_SUFDIR','AP_UNITTYP','AP_UNIT','AP_BUILDIN','AP_FULLADD','AP_CITY','AP_STATE','AP_ZIP']) as rows:
    for row in rows:    
        d2[row[0]] = [a.upper() for a in row[1:] if a.strip() != '']

print(d2)


#arcpy.AddField_management(dbf1,'APNOTEXIST','STRING')
##Gets the difference detween dictionary two and dictionary 1
##https://stackoverflow.com/questions/34298613/python-find-difference-between-two-dictionaries-containing-lists
diff = {}

for key in d1:
    diff[key] = list(set(d2[key])-set(d1.get(key,[])))
print(diff)



with arcpy.da.UpdateCursor(dbf1, ['FI_PATH', 'FnotExist']) as cursor:
    for row in cursor:
        keyValue = row[0]
        if keyValue in diff:
            row[1] = diff[keyValue]
            cursor.updateRow(row)

Below is an example of the dictionary, named diff, that I'm using to update the field FnotExist.

{'C:\\TestFolder\\Folder1\\A\\address_pt.shp': ['TYPEB', ' BUILDINGA'], 'C:\\TestFolder\\Folder1\\B\\ADDRESS_VALID_POINTS.shp': [' PRETYPEA'], 'C:\\TestFolder\\Folder1\\E\\Public_Safety_Addresses.shp': [' TYPEA']}

Below is a pic of the dbf table that I'm trying to update. The field FI_PATH was used to create the keys in the dictionary so keys should match values in row. enter image description here

I'm not sure why I'm getting an error and after research I can't figure out how to structure the code within the UpdateCursor.

3
  • 3
    What type of field is FnotExist? If I understand your example dictionary, you're trying to populate this field with a List (rather than a String of a list...)
    – Midavalo
    Feb 1, 2020 at 4:55
  • 2
    I agree. Try converting to string: row[1]=', '.join(diff[keyValue])
    – BERA
    Feb 1, 2020 at 8:04
  • 1
    Yes, I'm trying to populate the field FnotExist that accepts strings with a dictionary value that is a list of strings. @BERA Your suggestion worked. I replaced row[1] = diff[keyValue] with row[1]=', '.join(diff[keyValue]) Feb 1, 2020 at 14:13

1 Answer 1

1

As already commented and your error message suggests, you are trying to write lists (=list data type) to a string field. Convert the list to a comma seperated string with join:

row[1]=', '.join(diff[keyValue])

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.