I get invalid syntax

2049
6
Jump to solution
03-09-2016 06:19 PM
G__EricCastillo_Morales
New Contributor III

I 'm trying to run my script but I get this error message.

syntax.jpg

Here is the code.

  1. import arcpy
  2. from arcpy import env
  3. env.workspace = r'D:\Eric\Ejercicio\shp\ejercicioTest.gdb\shp'
  4. crbuff = r'D:\Eric\Ejercicio\shp\ejercicioTest.gdb\shp\CRojaPBuff'
  5. insumo = r'D:\Eric\Ejercicio\shp\ejercicioTest.gdb\shp\Insumo'
  6. # Add field to insumo.
  7. arcpy.AddField_management(insumo, "test", "TEXT", "", "", "50", "", "NULLABLE", "NON_REQUIRED", "")
  8. # Make a layer from crbuff to get the select by location works.
  9. arcpy.MakeFeatureLayer_management (crbuff, 'crbuff_layer')
  10. arcpy.MakeFeatureLayer_management (insumo, 'insumo_layer')
  11. # loop with cursor
  12. cursor = arcpy.SearchCursor(crbuff)
  13. field = "Lab" # Specify the field in the loop.
  14. for row in cursor:
  15.     print (row.getValue(field))
  16.     arcpy.SelectLayerByLocation_management('insumo_layer', "HAVE_THEIR_CENTER_IN", 'crbuff_layer', "", "NEW_SELECTION", "NOT_INVERT")
  17.     # Process: Calculate Field.
  18.     cursorIn = arcpy.UpdateCursor('insumo_layer')
  19.     for row in cursorIn:      
  20.         row.setValue("test", (row.getValue(field))
  21.         cursorIn.updateRow(row)
  22. # Process: Summary Statistics.
  23. intable = r'D:\Eric\Ejercicio\shp\Insumo.shp'
  24. outtable = r'D:\Eric\Ejercicio\shp'
  25. arcpy.Statistics_analysis(intable, outtable + "stats.dbf", "POB1 SUM;POB8 SUM;POB11 SUM;POB14 SUM;POB15 SUM;POB16 SUM;POB24 SUM;POB31 SUM;POB57 SUM;MIG7 SUM;INDI1 SUM;DISC1 SUM;DISC4 SUM;DISC5 SUM;DISC6 SUM;ECO1 SUM;ECO4 SUM;ECO25 SUM;ECO28 SUM;SALUD1 SUM;SALUD2 SUM;SALUD3 SUM;SALUD4 SUM;SALUD5 SUM;SALUD6 SUM", "test")

I need help ...

0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor
Extra bracket in 23.   row.setValue("test", (row.getValue(field))

Change it to: row.setValue("test", row.getValue(field))

Also, consider using arcpy.da.Search/InsertCursor instead of arcpy.Search/InsertCursor, the new (at 10.1) cursors are much faster and have easier and more "pythonic" syntax.

View solution in original post

6 Replies
Luke_Pinner
MVP Regular Contributor
Extra bracket in 23.   row.setValue("test", (row.getValue(field))

Change it to: row.setValue("test", row.getValue(field))

Also, consider using arcpy.da.Search/InsertCursor instead of arcpy.Search/InsertCursor, the new (at 10.1) cursors are much faster and have easier and more "pythonic" syntax.

View solution in original post

AdrianWelsh
MVP Notable Contributor

Luke, nice catch. Also, how do you know when to use arcpy.da.SearchCursor versus using arcpy.SearchCursor?

0 Kudos
XanderBakker
Esri Esteemed Contributor

In most cases I would recommend using the da cursors, since they are much faster. I can only think of a few use cases for the "old" cursors like accessing the attribute table of the integer raster data set or when using ArcGIS 10.0. There is some more info here: Accessing data using cursors—Help | ArcGIS for Desktop 

G__EricCastillo_Morales
New Contributor III

Thanks Luke. That corrects the syntax problem. Now I get a new problem.

Traceback (most recent call last):

  File "D:\Eric\Ejercicio\script\again.py", line 23, in <module>

    row.setValue("test", row.getValue(field))

  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\arcobjects\arcobjects.py", line 1096, in getValue

    return convertArcObjectToPythonObject(self._arc_object.GetValue(*gp_fixargs(args)))

RuntimeError: ERROR 999999: Error executing function.

I'm trying to write in "test" field from insumo layer the value field from the select by location features loop of crbuff layer.

0 Kudos
G__EricCastillo_Morales
New Contributor III

I'm trying to write in test field from some layer based on iterate feature selction. I get succes using model builder but I'm fail when I try with my own code.

The process with MB.

mb.jpg

The code and error.

mycode.jpg

Any suggestions?

0 Kudos
G__EricCastillo_Morales
New Contributor III

I decided to put this in another question. Thanks.

0 Kudos