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.