Select by attribute using a variable

932
2
03-07-2013 05:32 PM
babakkasraei
New Contributor II
Hi

I have created the following script to select the biggest extent in the mapview using an select by attribute tool which must contain an expression for where clause.

I have also followed other posts like Kirsten 's post who has tried to solve exactly the same problem.

My problem is that the expression doesn't work.



 # Import modules
import sys,os,math,string,arcpy
from arcpy import env

# Read from current map
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Data Themes")[0]
lyr = arcpy.mapping.ListLayers(mxd, "CHS_Chart_Extents", df)[0]
#The DataFrame extent object is converted into a polygon feature so it can be used with the SelectLayerByLocation function.
dfAsFeature = arcpy.Polygon(arcpy.Array([df.extent.lowerLeft, df.extent.lowerRight, df.extent.upperRight, df.extent.upperLeft]),
                            df.spatialReference)
# Select the extents into the df map view
arcpy.SelectLayerByLocation_management(lyr, "WITHIN", dfAsFeature, "", "NEW_SELECTION")


 # Create the search cursor
#
cur = arcpy.SearchCursor(lyr)
# This list will keep the numbers
featureList = []
#This loop will go through rows to find chart Numbers 
for row in cur:

   featureList.append(row.CHARTSCALE)

# This is the maximum scale in mapview
MX = max(featureList)
# this message says that the script is OK so far
arcpy.AddMessage(max(featureList))

# Now I need to select only the biggest extent in the mapview with maximum extent
arcpy.SelectLayerByAttribute_management (lyr, "NEW_SELECTION",  "CHARTSCALE = '" + MX + "'")



My problem is here


# This is the maximum scale in mapview
MX = max(featureList)

# Now I need to select only the biggest extent in the mapview with maximum extent
arcpy.SelectLayerByAttribute_management (lyr, "NEW_SELECTION",  "CHARTSCALE = '" + MX + "'")



How sould I write my where clause to work?

Thanks
Tags (2)
0 Kudos
2 Replies
Luke_Pinner
MVP Regular Contributor
For future reference, "doesn't work" is not useful and makes it difficult for people to answer your question. What was the problem exactly - i.e. did you get an error message, did your script crash, what line did it crash on, did python crash with some sort of windows error, did your computer reboot, did smoke come out of your computer vents, etc...? 😉

Luckily, I think I can see your problem. MX is a number, you can't add a number to a string, you'll get a TypeError. You can use the following instead:
"CHARTSCALE = '" + str(MX) + "'"
#or
"CHARTSCALE = '%s'"%MX
0 Kudos
babakkasraei
New Contributor II
For future reference, "doesn't work" is not useful and makes it difficult for people to answer your question. What was the problem exactly - i.e. did you get an error message, did your script crash, what line did it crash on, did python crash with some sort of windows error, did your computer reboot, did smoke come out of your computer vents, etc...? 😉

Luckily, I think I can see your problem. MX is a number, you can't add a number to a string, you'll get a TypeError. You can use the following instead:
"CHARTSCALE = '" + str(MX) + "'"
#or
"CHARTSCALE = '%s'"%MX


Hi Luck

You are absolutely right.

I had to describe what exactly happened.

Thanks for the codes. I will try them and thank you for your reminder.

Cheers

Babak
0 Kudos