Select to view content in your preferred language

Problem with SelectByAttribute

5075
14
01-13-2015 07:16 AM
SaidAkif
New Contributor III

Hi

I made a script that use arcpy.SelectLayerByAttribute_management.

The problem that some of the unique values of the field used in this selection have a special character (') ffor example : Gwich'in Conservation land

I know that the " is the problem because I removed the in from the field and the problem happened but when I removed the ' the script run perfectly

the selection was made automatically. I locate my field first after a function put all unique values in a table. an example of my code is in the txt file attached.

I believe that I have to change something in my expression.

Can you please help me to resolve this issue?

Thanks

0 Kudos
14 Replies
SaidAkif
New Contributor III


thanks for your answer

Here is my variable that will contain the value = "Gwich'in Conservation area" : "'" + str(ListUVCriteriaF) +  "'"

When I added """ in both side of my variable (""" + "'" + str(...) + "'" + """) it did not work (all thsi line in green so python did not recognize the variable.

I need a way to add the """

Thanks

0 Kudos
JamesCrandall
MVP Frequent Contributor

Your variable needs to be like this in your expression:

Gwich''in

Do you see the two (2) apostrophe's?  That's how it must be set.  How you deal with setting a variable will be up to you because you will need to check to see an apostrophe exists and if so then double it up.  I cannot understand the code you posted.

0 Kudos
SaidAkif
New Contributor III

Hi
Indeed, when I replace  ' by '', it works. I used the last Joshua code.

Thanks for your help guys

0 Kudos
SaidAkif
New Contributor III

ErrorMessage.jpg
Sorry. Here is the error message

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I revisited the Help for Building a query expression, where it states:

If the string contains a single quote you will first need to use another single quote as an escape character.

In Python, the backslash is the dominant escape character, but there are some additional escape sequences.  My sample code above that used text replacement to escape the single quote used the backslash, but ArcGIS wants users to escape a single quote by using another single quote.

The updated code worked for an example I ginned up on my machine:

expression = "{} = '{}' AND {} = '{}'".format(SelectCriteriaF
                                             str(list_UV_CriteriaF).replace("'","''"),  
                                             SplitCriteria,  
                                             str(listSplit).replace("'","''")) 
0 Kudos