Defintion query with apostrophe name

5668
6
Jump to solution
05-29-2015 11:59 AM
Pierre-LucBoivin
Occasional Contributor

Hi,

I'm trying to create an sql expression with a variable "arrondissement" but there's a problem with some attributes that contain apostrophe (quote) in their names like :

Baie d'Urfé or L'île-Bizard-Sainte-Geneviève.

I've try a few things but i'm always getting an error due to the apostrophe. It's work fine with other names that doesn't have quote.

lyr.definitionQuery = '"Arrondisse"' + "=" + "'" + arrondissement + "'"
or
lyr.definitionQuery = '"Arrondisse"' + "=" + "'\"%s\"'" %(arrondissement)

Thanks you for your help

0 Kudos
1 Solution

Accepted Solutions
RichardFairhurst
MVP Honored Contributor

Try:

lyr.definitionQuery = '"Arrondisse"' + "=" + "'" + arrondissement.replace("'","''") + "'"

If any apostrophes exist in the field's value each apostrophe will be doubled (no matter how many apostrophes are stored in the actual field value).  If no apostrophe exists the field's value will be unchanged.  Then you do not need to capture the values that contain apostrophes in any separate logic.

View solution in original post

6 Replies
DanPatterson_Retired
MVP Emeritus

no sure if this is your desired output but you probably need to express it in Unicode.  You can play around with this for testing

>>> x = u"Baie d'Urfé or L'île-Bizard-Sainte-Geneviève"
>>> x
u"Baie d'Urf\xe9 or L'\xeele-Bizard-Sainte-Genevi\xe8ve"
>>> print x
Baie d'Urfé or L'île-Bizard-Sainte-Geneviève
>>> z = '"Arrondisse"' + "=" + "'" + x + "'" 
>>> z
u'"Arrondisse"=\'Baie d\'Urf\xe9 or L\'\xeele-Bizard-Sainte-Genevi\xe8ve\''
>>> print z
"Arrondisse"='Baie d'Urfé or L'île-Bizard-Sainte-Geneviève'
>>>
0 Kudos
Pierre-LucBoivin
Occasional Contributor

Thanks for you answer Dan,

I found the reason why my script doesn't work,  it's because Arcgis double my apostrophe in the attribute.

So i'm parsing this string to definition query in my script.

"Arrondisse"='Baie-d'Urfé'

and when I open the attributes tables my table is empty.

When i'm doing an selection by attribute and I double the apostrophe, it's works.

"Arrondisse" = 'Baie-d''Urfé'

but I can't figured out how to write it and make it works in my script.

if lyr.name == "Ecoles":
                    lyr.definitionQuery = '"Arrondisse"' + "=" + "'" + arrondissement + "'" 
                    if lyr.definitionQuery == """"Arrondisse"=u'Baie-d'Urfé'""":
                        lyr.defintionQuery = """"Arrondisse"=u'Baie-d''Urfé'"""

UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal

  if lyr.definitionQuery == """"Arrondisse"=u'Baie-d'Urfé'""":

Thanks

0 Kudos
LukeWebb
Occasional Contributor III

I would try one of these:

#1 -  Use different types of quote. (EG Outer double quotes, inner single quote)
print "Hello there I'm Luke"


#2 - Use Triple quotes
print '''Hello there I'm Luke'''

#3 - Use ASCI Codes
print "Hello there I" + chr(39) + "m Luke"
0 Kudos
SepheFox
Frequent Contributor

Try it like this (by the way, you have a misspelling in the word "definition" on line 4):

  1. if lyr.definitionQuery == """"Arrondisse"=u"Baie-d'Urfé""""
  2.                         lyr.definitionQuery = """"Arrondisse"=u"Baie-d''Urfé"""" 
RichardFairhurst
MVP Honored Contributor

Try:

lyr.definitionQuery = '"Arrondisse"' + "=" + "'" + arrondissement.replace("'","''") + "'"

If any apostrophes exist in the field's value each apostrophe will be doubled (no matter how many apostrophes are stored in the actual field value).  If no apostrophe exists the field's value will be unchanged.  Then you do not need to capture the values that contain apostrophes in any separate logic.

Pierre-LucBoivin
Occasional Contributor

It's work perfectly with the replace

Thanks you for all you answer

0 Kudos