Python Triple Quotes Escape Stroke

4257
5
Jump to solution
10-01-2014 01:27 PM
GeoffOlson
Occasional Contributor

I'm using Python to run a script, but I've run into a small problem.  I'm using triple quotes to form a definition query, but inside the query is a wildcard %, so the interpreter is expecting a variable to format the string.  Is there a stroke key that can be used?

mapLyr2.definitionQuery = """"FID" IN (%s) AND "Jurisdicti" NOT LIKE '% Township'""" %dqStr

0 Kudos
1 Solution

Accepted Solutions
SusanJones
Occasional Contributor II

Geoff:

Try breaking your query string up and using a combination of ' and ".

e.g.

queryString = r'"FID" in (%s) and "Jurisdicti" like ' + '\'\% Township\''

View solution in original post

0 Kudos
5 Replies
SusanJones
Occasional Contributor II

Geoff:

Try breaking your query string up and using a combination of ' and ".

e.g.

queryString = r'"FID" in (%s) and "Jurisdicti" like ' + '\'\% Township\''

0 Kudos
JasonScheirer
Occasional Contributor III

Use %%.

>>> """x like 'hello%' and y = %s"""
"x like 'hello%' and y = %s"
>>> """x like 'hello%' and y = %s"""%"what"
Traceback (most recent call last):
  File "", line 1, in ValueError: unsupported format character ''' (0x27) at index 14
>>> """x like 'hello%%' and y = %s"""%"what"
"x like 'hello%' and y = what"
curtvprice
MVP Esteemed Contributor

I've become more of a fan of the Python 2.x/3.x string formatting:

mapLyr2.definitionQuery = """"FID" IN ({}) AND "Jurisdicti" NOT LIKE '% Township'""".format(dqStr)

I like to break things up into multiple lines with () to make it easier to read and debug. Note that if you place parentheses around the expression, it will get glommed together for you into a single string for .format() to process - look ma, no line continuation characters! So elegant.

mapLyr2.definitionQuery = (

    """"FID" IN ({}) AND """

    """"Jurisdicti" NOT LIKE '% Township'""").format(dqStr)

I'm with Susan -- I also find triple quotes kind of clunky - but this is of course totally personal preference!

mapLyr2.definitionQuery = (

    '"FID" IN ({}) AND '

    '"Jurisdicti" NOT LIKE \'% Township\'').format(dqStr)

GeoffOlson
Occasional Contributor

Thank you all for the replies.  I will keep this in mind.  Triple quotes was my immediate solution when I realized I had both single and double quotes for the query.  My temporary solution for this was to create a string variable that was the entire wildcard part '% Township' and that worked, but I don't want to keep creating variables just for the purpose of a workaround.

0 Kudos
JuanOrozco
Occasional Contributor

Another option is to use the ascii code for the character you're having trouble with. chr(37) would give you %; you just need to place it in the query string. You can deal with combinations of double and single quotes using the same approach, but it can get confusing in long queries.

mapLyr2.definitionQuery = 'FID IN ('+chr(37)+'s) AND '+chr(34)+'Jurisdicti'+chr(34)+' NOT LIKE ' + chr(37)+chr(39)+' Township'+chr(39)

or

mapLyr2.definitionQuery = 'FID IN ('+chr(37)+'s) AND '+chr(34)+'Jurisdicti'+chr(34)+' NOT LIKE ' + chr(37)+"'" Township"'"