I have a dbf table with two fields of interest. Field one is named AP_ZIP and rows contain field names for a zip code field within shapefiles. In addition, Field two is named APFI_PATH and contains the file path to a shapefile. I am using these two fields as parameters for arcpy.da.SearchCursor
In the code below, I created a list of file paths for shapefiles called filepathlist and a list of corresponding field names within the shapefile called zipfieldlist.
I'm trying to feed the file path from filepathlist and corresponding field name from zipfieldlist into the arcpy.da.SearchCursor and populate a dictionary. I want the dictionary to have file paths as keys and row items from fields as values.
import arcpy
arcpy.env.workspace = r"C:\AP_PAR"
dbf = "APExists_Test.dbf"
appath = 'APFI_PATH'
zipfield = 'AP_ZIP'
filepathlist =[row[0] for row in arcpy.da.SearchCursor(dbf,'APFI_PATH')]
print(filepathlist)
zipfieldlist =[row[0] for row in arcpy.da.SearchCursor(dbf,'AP_ZIP')]
print(zipfieldlist)
ZipDict = {}
for fp in filepathlist:
for fields in zipfieldlist:
with arcpy.da.SearchCursor(fp,[fields]) as curs:
for row in curs:
keyvalue = fp
ZipDict[keyvalue] = [row[0]]
#print(row[0])
print(ZipDict)
When I run the code I get the following error.
However, if I uncomment print(row[0])
, the code runs and starts to print out zip codes. In addition, the dictionary ZipDict gets populated with one and only one key:value pair {'C:\\AP_PAR\\AL\\tuscaloosa\\E911_GDB_SDE_address.shp': [('35404',)]}
len = 1
My desired output is to have a dictionary with keys as APFI_PATH and values as AP_ZIP. I want the values to be a list of strings. Ideally I want the values to be integers and not strings, but one step at a time.
Desired Output Example: Key = APFI_PATH and Value = AP_ZIP (list of strings)
{'C:\AP_PAR\AL\tuscaloosa\E911_GDB_SDE_address.shp': ['35404','35401','35402', etc....]}
Any ideas as to why I am getting this error and how to fix the code?
dict
with{filepath: ['zipfield1, ..., 'zipfieldN']}