I'm a total beginner at programming and Python. I'm trying to select features by attributes and then field calculate in another field.
In VBA it might look like this:
If [Z1COMM] = "R" then
[Z1UNITS] = [Z1RUNTS] and
[Z1SIZE] = [Z1RSIZE]
ElseIf [Z1COMM] = "O" then
[Z1UNITS] = [Z1OUNTS] and
[Z1SIZE] = [Z1OSIZE]
Else [Z1UNITS] = [Z1RUNTS] and
[Z1SIZE] = [Z1SIZE]
End If
I could probably do this pretty easily in VBA using the code block in the field calculator but I want to start grasping Python more. Here's what I got from trying to make a list of the values as a first step (taken and modified from http://support.esri.com/en/knowledgebase/techarticles/detail/41442):
# Import modules
import arcpy, os , sys, string
# Create environmental variables
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"J:\CO_004"
# Set variables
input = "testfile.shp"
lyr = "tst_layer"
arcpy.MakeFeatureLayer_management(input,lyr)
outFile = open(r"J:\CO_004\test.text", "w")
# Build search cursor
fcSearch = arcpy.da.SearchCursor(lyr, ["Z1COMM"])
for fcRow in fcSearch:
# Process: Select by attributes
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", "Z1COMM" == 'O')
txtSearch = arcpy.da.SearchCursor(lyr, ["Z1COMM"])
outFile.write("Commodity: " + str(fcRow[0]) + "\n")
for txtRow in txtSearch:
zval = str(txtRow[0])
outFile.write(zval + "\n")
outFile.close() #This closes the text file
del input, lyr, fcRow, txtRow, txtSearch, fcSearch, outFile, zval
And this is my latest error: ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).
I originally tried to use a feature class for the input to avoid this part of the code: lyr = "tst_layer" arcpy.MakeFeatureLayer_management(input,lyr)
But got an error that feature classes could not be used. If I have to make a separate shapefile so be it but it's be more convenient to work straight from a feature class in a file GDB.
I'm mainly confused by this part:
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", "Z1COMM" == 'O')
where the last parameter I thought should be the selection criteria.
Also, this code just makes a txt file, it doesn't actually calculate anything but I thought once I figured this out I could take the next step.
-
1Your expression is incorrect, it should be "Z1COMM = 'O'", you could also do that with your cursor 'where_clause' eliminating the need for select by attribute... consider also using a with block instead of del, it's a bit neater: with arcpy.da.SearchCursor....: cleans it up automatically on dedent but doesn't work with arcpy.SearchCursorMichael Stimson– Michael Stimson2015年09月09日 22:33:27 +00:00Commented Sep 9, 2015 at 22:33
1 Answer 1
Instead of:
"Z1COMM" == 'O' # will always evaluate to False because "Z1COMM" != 'O'
Try this:
""""Z1COMM" = '{0}'""".format("O")
There are a number of ways this can be done.
-
1If the quoting is required "\"Z1COMM\" = 'O'" is the way I usually do it, but the best way is with arcpy.AddFieldDelimeters resources.arcgis.com/de/help/main/10.1/index.html#//…. I have found that the quotes and square brackets are not necessary in field names except in joined tables and calculate field.Michael Stimson– Michael Stimson2015年09月09日 22:39:27 +00:00Commented Sep 9, 2015 at 22:39
-
I ran it with the first change and it worked except that the cursor is going through each record one at a time, which it's supposed to do but it takes forever to run (I have 45,000 or so records). Is it possible for the script to first make a selection based on a SQL query and then use the updateCursor to change the desired fields on the whole selection at once instead of going through each row one at a time?geoJshaun– geoJshaun2015年09月11日 21:11:35 +00:00Commented Sep 11, 2015 at 21:11
-
@Shaun for that you would not use a cursor. Try Select By Attribute then Calculate Field instead.2015年09月11日 21:55:49 +00:00Commented Sep 11, 2015 at 21:55
-
Yes, I could use Select By Attributes and then the Field Calculator but I would have to do it several times over. Eventually this will be part of a much larger script where I will have to populate two separate fields from values in several different fields depending on the Z1COMM value, once the values are calculated the program will break up the feature class into new ones based on Z1COMM and change the symbology for each new layer.geoJshaun– geoJshaun2015年09月11日 22:05:22 +00:00Commented Sep 11, 2015 at 22:05
-
@Shaun not the Field Calculator, there is a Calculate Field tool you can use in scripts etc2015年09月11日 22:15:20 +00:00Commented Sep 11, 2015 at 22:15
Explore related questions
See similar questions with these tags.