I have a dBase database file and I'm viewing and editing the table in ArcGIS Pro. I want to use the arcpy.da.SearchCursor function to fill a dictionary and create a key-value pair by iterating through multiple fields and row values in my table. I will eventually use this dictionary to compare key-value pairs from another dictionary and see if items in both lists match.
My first step is to understand and create dictionaries with ArcPy and Python. My goal is to create and fill a dictionary where the key is a shapefile and the value is a list of field names thought to be within the shapefile. The code below creates a key and value pair where the key is a shapefile and the value is a list of row attributes from five fields.
import arcpy
arcpy.env.workspace = r"C:\TestFolder"
dbf = "APExistOnly_Edit.dbf"
myfield = "FI_PATH"
FieldNameDict = {}
rows = arcpy.SearchCursor(dbf)
for row in rows:
FieldNameDict[row.FI_PATH] = [row.AP_ZIP,row.AP_STATE,row.AP_CITY,row.AP_FULLADD,row.AP_BUILDIN]
print(FieldNameDict)
With this code I created a dictionary called FieldNameDict and placed the key as field FI_PATH and assigned the row values from each field to a list. Below is the output from the code. When viewing the output, one can see that the lists contain empty elements with no text. This is because some of the fields contain rows with no value.
Currently, I have messy code and logic and I want to see if someone can help with better code. I want to create the dictionary by using arcpy.SearchCursor and iterate through multiple fields and rows. I want to create lists, associated with a key, that do not include empty row elements ' ' . I would like to exclude rows with no text when iterating through fields with the search cursor object.