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
Solved! Go to Solution.
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.
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' >>>
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
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"
Try it like this (by the way, you have a misspelling in the word "definition" on line 4):
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.
It's work perfectly with the replace
Thanks you for all you answer