1

I am trying to update a field with a new value as I updated a Domain for a web purpose. I am new to this.

I want be able to choose the layer and then the field to update

M=Municipal
S=State
C=County
P=Private

Here is the script so far.

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# Ownership Domain Update.py
# Created on: 2016-11-15 11:57:22.00000
#   (generated by ArcGIS/ModelBuilder)
# Usage: Ownership Domain Update <FClass>
# Description:
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

# Script arguments
FClass = arcpy.GetParameterAsText(0)
FieldName = arcpy.GetParameterAsText(1)

# Process: Select Municipal
arcpy.SelectLayerByAttribute_management(FClass, "NEW_SELECTION", FieldName  = 'M')

# Process: Calculate Municipal
arcpy.CalculateFieldName_management(FClass, FieldName , "\"Municipal\"", "VB", "")

# Process: Select State
arcpy.SelectLayerByAttribute_management(FClass, "NEW_SELECTION", FieldName  = 'S')

# Process: Calculate State
arcpy.CalculateFieldName_management(FClass, FieldName , "\"State\"", "VB", "")

# Process: Select County
arcpy.SelectLayerByAttribute_management(FClass, "NEW_SELECTION", FieldName  = 'C')
# Process: Calculate County
arcpy.CalculateFieldName_management(FClass, FieldName , "\"County\"", "VB", "")

# Process: Select Private
arcpy.SelectLayerByAttribute_management(FClass, "NEW_SELECTION", FieldName  = 'P')

# Process: Calculate Private
arcpy.CalculateFieldName_management(FClass, FieldName , "\"Private\"", "VB", "")

# Process: Clear
arcpy.SelectLayerByAttribute_management(FClass, "CLEAR_SELECTION", FieldName  = 'P')

And the Error:

ERROR:
raceback (most recent call last):
  File "G:\_LSSE GIS Toolbox\_LSSE Toolbox\Ownership Domain Update.py", line 18, in <module>
    arcpy.SelectLayerByAttribute_management(FClass, "NEW_SELECTION", FieldName  = 'M')
TypeError: SelectLayerByAttribute() got an unexpected keyword argument 'FieldName'

Failed to execute (Script).
Failed at Tue Nov 15 14:14:33 2016 (Elapsed Time: 0.06 seconds)
  • You need to enclose the sql queries, for example Fieldname='M', in " or ' signs, check out help section for the tool – BERA Nov 15 '16 at 19:54
1
# Process: Select Municipal
arcpy.SelectLayerByAttribute_management(FClass, "NEW_SELECTION", "FieldName  = 'M'")

BERA is correct, your SQL statement needs to be a string like I have adjusted above. Because your "FieldName" is a variable, you will probably need do it like this:

# Process: Select Municipal
arcpy.SelectLayerByAttribute_management(FClass, "NEW_SELECTION", "{0}  = 'M'".format(FieldName))

The .format is just a way to substitute your value into a string, you can use as many substitutions as you want in one string, the number in the brackets indicates the position for each, so you could do something like this:

var1 = "Hello"
var2 = "!"
"{0} world{1}".format(var1, var2)

It's a handy trick that I use a lot in my scripting

  • @GeoSharp I see that in the examples ESRI has on the select by location help page you are correct, but interestingly I have never done that in my scripts and I've never had problems... Does it really make a difference? Or do you think the need for them was eliminated by some clever ESRI scripting at some point? – maverickGIS Nov 15 '16 at 22:09
0

Your entire where clause needs to be surrounded by quotations either "" or ''. Within the where clause, your criteria need to be surrounded by the correct syntax quotations. The correct syntax is '' for the criteria. Like this:

arcpy.SelectLayerByAttribute_management("GNSS_Results_Stations", "NEW_SELECTION", "\"FieldName\" = 'M'")

Because the criteria of the where clause should be surrounded by '' it is easiest to use "" for the entire where clause.

Some parsers also require that the field name be surrounded by the correct syntax quotations, "" for fields (as shown above). However I don't believe this to be the case when using arcpy.SelectLayerByAttribute_management. For more specifics on that arcpy command you can review the documentation here. For more information on how to structure the where clause see this GIS Stack Exchange question and answer here.

Additionally you probably should use a more descriptive variable or formatted differently variable name than FieldName. I don't think it is a keyword but to avoid confusion often times python variables are designated with only lower case and separated by an underscore, e.g. field_name.

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.