Select by Attribute and Calculate from old AML

520
5
06-19-2013 10:54 AM
AmyDoyle
New Contributor
Hello,

I am not a person who works with Python but I am trying to convert an old AML into Python. Right now I am going line by line in the command prompt to see what works and then save that to a file.  What i need to do is a simple select by attribute and calculate an item based on the selected set. Is there no straight forward way to do this?  I am in v 10.1 if that matters.

the original AML querry and calc was simple
Select:

>= 30 AND (( [PF] is null ) AND  ( [MC] = 300 ))


Calc: [PF] = 'Char'


To attempt to duplicate this in Python, I've done the following:

import arcpy
FPlan = "C:\\path\<mine>.mdb\\FP
arcpy.MakeFeatureLayer_management = (FPlan, "F")
arcpy.SelectLayerByAttribute_management ("F", "NEW_SELECTION", "

>= 30 AND ( [PF] = '' AND [MC] = 300) ")


- I think this is selecting items, all it returns is <Result 'F'>

arcpy.CalculateField_management(calcF, "P", "Char")
-and this statement errors out.


Please, can anyone tell me how to determin if A) features are selected and B) how to correctly calc the selected set into the
item

.



Thank you very much!
Amy
Tags (2)
0 Kudos
5 Replies
T__WayneWhitley
Frequent Contributor
You can bypass the Select Layer By Attributes execution because Make Feature Layer has an parameter in which to enter your query statement...pass that layer to your Calculate Field execution.  Also, if you know how many features the query results in, it's a good idea to test that with GetCount_management.

Hope that helps...also see the webhelp for setting up queries, there are both Python and VBScript (and I think JScript) sytax guidance.


Enjoy,
Wayne

PS: Although there are various techniques for specifying a query, see this:

Specifying a query in Python
Desktop » Geoprocessing » Python
http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000001r000000
0 Kudos
MathewCoyle
Frequent Contributor
First I would try to avoid personal geodatabases as support for them is waning.
Second you can use a cursor with a where clause and do it all in 4 lines.

cursor = arcpy.da.UpdateCursor(FPlan, ["PF"], '''"P" >= 30 AND (("PF" is null ) AND ("MC" = 300 ))''')
for row in cursor:
    row[0] = 'Char'
    row.updateRow(row)
0 Kudos
MichaelVolz
Esteemed Contributor
Matthew:

Do you have any idea when personal geodatabases will no longer be available?
0 Kudos
MathewCoyle
Frequent Contributor
Michael,

I'd imagine it would be when they switch desktop to 64-bit native, since as far as I know .mdb files cannot be accessed by 64-bit applications. Server already has dropped support for personal geodatabases because of this. The earliest I'd imagine this would happen would be the version after 10.2. Of course they may simply switch personal geodatabases to .accdb and retain support. It's anyones guess at this point though, I haven't heard anything official either way.
0 Kudos
curtvprice
MVP Esteemed Contributor
the original AML querry and calc was simple
Select:

>= 30 AND (( [PF] is null ) AND  ( [MC] = 300 ))
Calc: [PF] = 'Char'



Amy,

I knew AML. AML was a friend of mine. This is not AML. Avenue, VBA maybe?

Please, can anyone tell me how to determin if A) features are selected and B) how to correctly calc the selected set into the
item

.



Here's a direct answer to your question

arcpy.env.workspace = "myfile.mdb" 
pvar = arcpy.AddFieldDelimeters("P") # 
 for mdb, "P" otherwise
pfvar = arcpy.AddFieldDelimeters("PF")
mcvar = arcpy.AddFieldDelimeters("MC")
where = '{0} > 30 AND (({1} IS NULL) AND ({2} = 300))'.format(pvar,pfvar,mcvar)
lyr = arcpy.MakeFeatureLayer_management(features, "lyr", where)
result = arcpy.GetCount_management(lyr)
nselect = int(result.getOutput(0))
print str(nselect), " selected"
arcpy.CalculateField_management(lyr, "P", "'Char'", "PYTHON_9.3")
arcpy.Delete_management(lyr) # clean up
0 Kudos