Concatenate expressions issue

531
4
Jump to solution
11-23-2016 08:12 AM
jaykapalczynski
Frequent Contributor

I have two expressions that work independently but cant get the syntax correct to put them together. 

expression1 = "trailblazers = 'No' OR regulations = 'No' OR safteysign = 'No'"

expression2 = "date >= " + "'%s'" %startdate + " AND " + "date <= " + "'%s'" %enddate

I tried this:

expression3 = expression2 + ' AND ' + expression1

expression3 = expression2 + " AND " + expression1

expression3 = "date >= " + "'%s'" %startdate + " AND " + "date <= " + "'%s'" %enddate + " AND " +  "trailblazers = 'No' OR regulations = 'No' OR safteysign = 'No'"

When I run expression3 it seems to ignore expression2 and gives me all the dates not the ones specified.  Confused because when I run expression1 and expression2 individually they do their job.

Thoughts?

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

I don't know what you are going to do with this, but it will get messy if you don't start using brackets to enclose conditions.. As an example... not necessarily a solution to your problem... try to formulate conditions and hard to manage strings using formatting options designed to facilitate the process... the following is an example

>>> ex3 = "({}) AND ({})".format(expression1, expression2)
>>> ex3
"(trailblazers = 'No' OR regulations = 'No' OR safteysign = 'No') AND (date >= '1' AND date <= '2')"

View solution in original post

4 Replies
RebeccaStrauch__GISP
MVP Emeritus

There are many more ways that you could do the formatting, but I won't touch on that....but make sure you "math" is correct.....maybe add parenthesis in places?, e.g.

expression1 = "(trailblazers = 'No' OR regulations = 'No' OR safteysign = 'No')"

also, use print statements to see what the variables are actually storing.  And it's always good to test manually to see if you are getting the correct results.

DanPatterson_Retired
MVP Emeritus

I don't know what you are going to do with this, but it will get messy if you don't start using brackets to enclose conditions.. As an example... not necessarily a solution to your problem... try to formulate conditions and hard to manage strings using formatting options designed to facilitate the process... the following is an example

>>> ex3 = "({}) AND ({})".format(expression1, expression2)
>>> ex3
"(trailblazers = 'No' OR regulations = 'No' OR safteysign = 'No') AND (date >= '1' AND date <= '2')"
jaykapalczynski
Frequent Contributor

THANKS DAN....that worked great....still learning day by day here.....have a great Thanksgiving.

0 Kudos
XanderBakker
Esri Esteemed Contributor

As a side note, be aware that the field delimiters will change depending on the data source. In your case you probably work with an enterprise gdb since the fields do not have any double quotations (fgdb and shapefiles) or square brackets (personal geodatabases). In case you want to make it compatible regardless of the data source (assuming that the field names are not to long) you could consider using the: AddFieldDelimiters—Help | ArcGIS for Desktop 

The help contains examples of how to implement it.