1

I'm trying to write a script that relates data from a .csv file to a utility pole shape file. Basically, we want the script to iterate through the pole # fields in each table and determine which pole numbers are missing from the .shp. Right now, the script iterates through every row of the field and appears to not be reading the pole # value inputted by the user, since every print out reads the pole # and NO (ex: 111A0111, NO) no matter if the pole # is matching or not. We want the program to read "YES" if the pole number is in both tables, and "NO" if it is only in the .csv file and not the .shp.

import arcpy
import csv


#define other variable
fc = "P:\Scripts\data_check\prd_poles.shp"
field = "GISID"

#Create a list to be populated by the search shapefile search cursor
gis_poles_assets =[]

cursor = arcpy.SearchCursor(fc)

#Populate the list
for row in cursor:
  gis_poles_assets.append(row.getValue(field))
#print gis_poles_assets


#define the csv to be read by the reader and make that a variable
f = open(r'P:\Scripts\data_check\discoverer_poles_snip.csv')
csv_f = csv.reader(f)

#search the shapefile based on fields in a .csv
for row in csv_f:
  if row [1] in gis_poles_assets:
   pass
  elif "111A0111" in gis_poles_assets:
    print row [1], "YES"

  else:
    print row [1], "NO"
  • We've got 10.6.1 – Mark1994 May 15 at 14:50
1

This should do the trick. It uses a pandas dataframe with the newer da.SearchCursor.

import arcpy
import pandas as pd


df = pd.read_csv(r"path", usecols = ["GISID"]) 
fc = r".shp"
fields = ['GISID']

gis_poles_assets =[]



with arcpy.da.SearchCursor(fc, fields) as cursor:
    for row in cursor:
        gis_poles_assets.append(row[0])


for index, row in df.iterrows():
    if not row['GISID'] in gis_poles_assets:
        print row['GISID'] + " No"
| improve this answer | |
  • Thank you! This works exactly how we need it to. We'll be able to apply this to a lot of our QA/QC processes now. – Mark1994 May 15 at 19:58
1

You are using the old cursor which is much slower than the data access cursor. Create a view of the csv and use cursors on both shape and view to list all values then compare using sets:

import arcpy

fc = r'C:\GIS\ArcMap_default_folder\Default.gdb\ak_riks'
fcfield = 'polenr'
poles_in_fc = [f[0] for f in arcpy.da.SearchCursor(fc, fcfield)]

csv = r'C:\GIS\data\testdata\plist.csv'
csvfield = 'polenr'
arcpy.MakeTableView_management(in_table=csv, out_view='csvview')
poles_in_csv = [f[0] for f in arcpy.da.SearchCursor('csvview',csvfield)]

print 'Poles in csv but not in fc: {}'.format(list(set(poles_in_csv)-set(poles_in_fc))) #https://www.geeksforgeeks.org/python-set-difference/

enter image description here

| 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.