2

I am fairly new to python scripting. Most of the basics I have down however still learning.

I am trying to create a python script to update a shapefile with information from two separate CSV files. There is a common attribute that will link the CSV records to the Shapefile. I want to update the records of the shapefile with information from provided in the CSV files.

I found a script from a website that appears to do what I am looking for (link below), however when I run the script it it errors on line 11: for row2 in cursor2 and states: "Runtime Error; Invalid SQL Statement". I have tried several different attempts to negate the issue to no avail. If anyone could point out what is wrong with the script or if you have a more efficient way to create a script to accomplish my goal I am all ears.

import arcpy
from arcpy import env
env.workspace = "C:/Data"

print 'Processing...'

fc = "parcels.shp"

cursor = arcpy.da.SearchCursor(fc, ["APN"])
for row in cursor:
  SQL_stat= "APN = "+ str(row[0])
  fc2 = "Numerical_CSV.csv" 

cursor2 = arcpy.da.SearchCursor(fc, ["ASSESSMENT"], SQL_stat)
for row2 in cursor2:
  UpdatedValue = row2[0]
  cursor3 = arcpy.da.UpdateCursor(fc2, ["TAXYEAR"],SQL_stat)
    for row3 in cursor3:
        row3[0] = UpdatedValue
        cursor3.updateRow(row3)
del row
del cursor
del row2
del cursor2
del row3
del cursor3

print "Done"
  • Welcome to GIS SE! As a new user be sure to take the Tour. Would you be able to use the edit button beneath your question to improve it by replacing the picture of your code with a code snippet as text so that it can be copy/pasted for testing and searched, please? – PolyGeo Jan 8 '16 at 2:51
  • this isn't what you're asking about, but would I recommend using with statements in your cursors so that you don't have to del them. – Paul H Jan 8 '16 at 3:22
  • 3
    also, I think the indentation at the start of the inner for loop is off. – Paul H Jan 8 '16 at 3:24
  • It's a pretty inefficient construct to initiate a cursor within each row of a cursor within each row of a cursor. I would recommend reading values into dictionaries before entering the update cursor. – Tom Jan 8 '16 at 5:53
  • 1
    I can't find any evidence that arcpy cursors work on csv files. You should read the values from the csv using python's built-in csv module. – Tom Jan 8 '16 at 5:59
2

When selecting a field in a shapefile you need to surround the field name (e.g. FID) with quotes:

SQL_stat = '"FID" = {}'.format(row[0])
  • 3
    In fact if you use AddFieldDelimiters, it will be less error-prone, such as SQL_stat= "{0} = {1}".format(arcpy.AddFieldDelimiters(fc,"APN"),str(row[0]) – fatih_dur Jan 8 '16 at 5:37

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.