0

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

enter image description here

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.

enter image description here

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

enter image description here

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?

  • Question was edited, much appreciated. Trying to get a dict with {filepath: ['zipfield1, ..., 'zipfieldN']} – QuagmaDatum Apr 11 at 3:18
1

You're using every single zipcode field in the searchcursor for each shapefile, which which is why you get the RuntimeError. I assume each shapefile only has a single zipcode field.

Try:

import arcpy

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

dbf = "APExists_Test.dbf"
appath = 'APFI_PATH'
zipfield = 'AP_ZIP'
ZipDict = {}

with arcpy.da.SearchCursor(dbf,[appath, zipfield]) as dbfrows:
    for fp, zf in dbfrows:
        with arcpy.da.SearchCursor(fp,[zf]) as shprows:
            ZipDict[fp] = [row[0] for row in shprows]
| improve this answer | |
  • Works great, much appreciated. Just had to change the line for fp, zf in rows: to for fp, zf in dbfrows: – QuagmaDatum Apr 14 at 0:46
  • Sorry, fixed now – user2856 Apr 14 at 1:05
1

You seem be expecting every shapefile to have all fields Zip5, Zipcode, and zip. If any one of these is not there, you will get the error. There may be different ways to do this, but you could use the Pythonic concept that it is easier to ask for forgiveness than permission and just skip if you get the error.

for row in curs:
    try:
        keyvalue = fp
        ZipDict[keyvalue] = [row[0]]
        #print(row[0])
    except:
        pass
| improve this answer | |

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.