python error select analysis invalid expression for a number variable

845
3
11-11-2013 12:18 PM
AndrewL
Occasional Contributor II
Hi I'm trying to do a select analysis with a number variable.

x=20131105
arcpy.Select_analysis(daily_stations, daily_stations_Select, "\"daily_YearMonthDay\" = x")


I've tried a number of different ways but keep getting errors. I obtained this code by building the select in modelbuilder and exporting to python. I replaced the number (20131105) that I used in modelbuilder with the variable x.

ERROR 000358: Invalid expression daily_YearMonthDay

Thank you
Tags (2)
0 Kudos
3 Replies
T__WayneWhitley
Frequent Contributor
If it is just a numerical field, you should be able to do it similar to this:

query = " \"daily_YearMonthDay\" = 20131105 "
arcpy.Select_analysis(daily_stations, daily_stations_Select, query)



...or, in keeping your variable ref 'x':
x=20131105
query = " \"daily_YearMonthDay\" = {0} ".format(x)
arcpy.Select_analysis(daily_stations, daily_stations_Select, query)


This is native Python, see:
http://docs.python.org/2/library/string.html
0 Kudos
AndrewL
Occasional Contributor II
Thank you! It worked. I kept the variable x.
0 Kudos
T__WayneWhitley
Frequent Contributor
Thank you! It worked. I kept the variable x.


That's great....don't forget to mark the question answered so I'll win a lifetime pass to the ESRI user conferences (okay, that latter part was pure fantasy, but you should get into the habit of properly marking answers so that this thread is considered 'closed'.)

Glad to assist!

...enjoy,
Wayne
0 Kudos