Select to view content in your preferred language

Update Cursor parsing error

957
6
Jump to solution
10-22-2013 02:12 PM
ClintonCooper1
Deactivated User
I am putting together some snippets of code and running into a parsing error: Parsing error SyntaxError: invalid syntax (line 8).  The error seems to be on line: updateRows = arcpy.da.UpdateCursor(mainTbl, ["FULL_ADDRESS","FREQUENCY"])
   I have tried all types of fixes, but cannot find the problem.  I am running on 10.2

import arcpy  lutTbl = r"Database Servers\CLINTONCOOPER_SQLEXPRESS.gds\VOTERS_20133 (VERSION:dbo.DEFAULT)\VOTERS_20133.DBO.frequency" mainTbl = r"Database Servers\CLINTONCOOPER_SQLEXPRESS.gds\VOTERS_20133 (VERSION:dbo.DEFAULT)\VOTERS_20133.DBO.OHIO_VOTERS_GEOCODE_OCT142013_small"  lutDict = dict([(r.FULL_ADDRESS, (r.FREQUENCY)) for r in arcpy.da.SearchCursor(lutTbl, ["FULL_ADDRESS","FREQUENCY"])  updateRows = arcpy.da.UpdateCursor(mainTbl, ["FULL_ADDRESS","FREQUENCY"]) for updateRow in updateRows:     nameValue = updateRow.FULL_ADDRSS      if nameValue in lutDict:         updateRow[1] = lutDict[nameValue][0]  #Address     else:     updateRows.updateRow(updateRow) del updateRow, updateRows 
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Esri Alum
You're mixing up arcpy.SearchCursor and arcpy.da.SearchCursor, among other issues (missing closing parens, indentation).
import arcpy  lutTbl = r"Database Servers\CLINTONCOOPER_SQLEXPRESS.gds\VOTERS_20133 (VERSION:dbo.DEFAULT)\VOTERS_20133.DBO.frequency" mainTbl = r"Database Servers\CLINTONCOOPER_SQLEXPRESS.gds\VOTERS_20133 (VERSION:dbo.DEFAULT)\VOTERS_20133.DBO.OHIO_VOTERS_GEOCODE_OCT142013_small"  lutDict = {r[0]: r[1] for r in arcpy.da.SearchCursor(lutTbl, ["FULL_ADDRESS","FREQUENCY"])}  with arcpy.da.UpdateCursor(mainTbl, ["FULL_ADDRESS","FREQUENCY"]) as updateRows:     for updateRow in updateRows:         nameValue = updateRow[0]         if nameValue in lutDict:             updateRow[1] = lutDict[nameValue][0]  #Address             updateRows.updateRow(updateRow)

View solution in original post

0 Kudos
6 Replies
JasonScheirer
Esri Alum
You're mixing up arcpy.SearchCursor and arcpy.da.SearchCursor, among other issues (missing closing parens, indentation).
import arcpy  lutTbl = r"Database Servers\CLINTONCOOPER_SQLEXPRESS.gds\VOTERS_20133 (VERSION:dbo.DEFAULT)\VOTERS_20133.DBO.frequency" mainTbl = r"Database Servers\CLINTONCOOPER_SQLEXPRESS.gds\VOTERS_20133 (VERSION:dbo.DEFAULT)\VOTERS_20133.DBO.OHIO_VOTERS_GEOCODE_OCT142013_small"  lutDict = {r[0]: r[1] for r in arcpy.da.SearchCursor(lutTbl, ["FULL_ADDRESS","FREQUENCY"])}  with arcpy.da.UpdateCursor(mainTbl, ["FULL_ADDRESS","FREQUENCY"]) as updateRows:     for updateRow in updateRows:         nameValue = updateRow[0]         if nameValue in lutDict:             updateRow[1] = lutDict[nameValue][0]  #Address             updateRows.updateRow(updateRow)
0 Kudos
ClintonCooper1
Deactivated User
With your changes, I am now getting a new error:

Runtime error
Traceback (most recent call last):
  File "<string>", line 12, in <module>
TypeError: 'int' object has no attribute '__getitem__


based on this row:


updateRow[1] = lutDict[nameValue][0]

0 Kudos
JasonScheirer
Esri Alum
updateRow[1] = lutDict[nameValue]


Still don't quite understand what you're doing here.
0 Kudos
ClintonCooper1
Deactivated User
The purpose of the code is to match the frequency table (convert it into a python dictionary), and then update a field in another file.  In other words it is a fast way of doing a join/calc.  If I understand the source where I got some of the code correctly, I am transferring the FREQUENCY filed data from the frequency.dbf (lutTbl) file into the mainTbl by way matching through the FULL_ADDRESS filed in each table.
0 Kudos
ClintonCooper1
Deactivated User
So I was able to get the code working (without any errors) after I removed the [0] after the lutDict[nameValue] in code row:

updateRow[1] = lutDict[nameValue]

However It is not updating any new data in the mainTble field.  Not sure where the code is going wrong.

Clinton
0 Kudos
ClintonCooper1
Deactivated User
You're mixing up arcpy.SearchCursor and arcpy.da.SearchCursor, among other issues (missing closing parens, indentation).
import arcpy

lutTbl = r"Database Servers\CLINTONCOOPER_SQLEXPRESS.gds\VOTERS_20133 (VERSION:dbo.DEFAULT)\VOTERS_20133.DBO.frequency"
mainTbl = r"Database Servers\CLINTONCOOPER_SQLEXPRESS.gds\VOTERS_20133 (VERSION:dbo.DEFAULT)\VOTERS_20133.DBO.OHIO_VOTERS_GEOCODE_OCT142013_small"

lutDict = {r[0]: r[1] for r in arcpy.da.SearchCursor(lutTbl, ["FULL_ADDRESS","FREQUENCY"])}

with arcpy.da.UpdateCursor(mainTbl, ["FULL_ADDRESS","FREQUENCY"]) as updateRows:
    for updateRow in updateRows:
        nameValue = updateRow[0]
        if nameValue in lutDict:
            updateRow[1] = lutDict[nameValue][0]  #Address
            updateRows.updateRow(updateRow)


This solution worked after I removed the [0] bracket and added the del updateRow, updateRows
0 Kudos