<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: selecting from a FGDB where the item has apostrophes in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/selecting-from-a-fgdb-where-the-item-has/m-p/52085#M4137</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;No that isn't it. The problem isn't in the ITEM NAME but rather in the value "John's Cabin"&lt;BR /&gt;because ArcMap is expecting single quotes to be around the value - but in this case the apostrophe messes that all up. &lt;BR /&gt;So far, using the double quotes in python doesn't seem to work nor does using the escape character \.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I t didn't work because there was an error in the first line suggested.&amp;nbsp; It should have been:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;qry = """Item_Name"" = 'John''s Cabin' &lt;STRONG&gt;"&lt;/STRONG&gt;&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The two single quotes together are SQL specific, not Python specific and that is how you include a single quote in an SQL query.&amp;nbsp; (Done it many times with an FGDB).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Feb 2012 21:02:32 GMT</pubDate>
    <dc:creator>RichardFairhurst</dc:creator>
    <dc:date>2012-02-10T21:02:32Z</dc:date>
    <item>
      <title>selecting from a FGDB where the item has apostrophes</title>
      <link>https://community.esri.com/t5/python-questions/selecting-from-a-fgdb-where-the-item-has/m-p/52082#M4134</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am trying to perform a SearchCursor or alternatively a SelectLayerByAttribute where the data will sometimes contain special characters, like apostrophes, dashes, and slashes. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm trying to figure out how to make the apostrophes work in my query. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;eg. &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;featureclass = r"C:\my feature class"
qry = "Item_Name = 'John's Cabin' "
arcpy.SearchCursor(featureclass, qry)
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt;that of course doesn't work. But I've tried a few different things, and none works. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I've tried: &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;qry = "John\'s Cabin"; John''s Cabin and neither work. Any other suggestions?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Feb 2012 20:05:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selecting-from-a-fgdb-where-the-item-has/m-p/52082#M4134</guid>
      <dc:creator>JoshuaChan</dc:creator>
      <dc:date>2012-02-10T20:05:39Z</dc:date>
    </item>
    <item>
      <title>Re: selecting from a FGDB where the item has apostrophes</title>
      <link>https://community.esri.com/t5/python-questions/selecting-from-a-fgdb-where-the-item-has/m-p/52083#M4135</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You need to enclose the field (Item_Name) in double quotes, the string value in single quotes (John's Cabin), definately escape the apostrophe with another one (John''s Cabin), and possibly wrap the whole thing in escaped double quotes. See &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Select_Layer_By_Attribute/001700000071000000/" rel="nofollow noopener noreferrer" target="_blank"&gt;this page &lt;/A&gt;&lt;SPAN&gt;(Chicago examples), and try it yourself in the Select Layer By Attribute toolbox tool expression builder. I would try:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;qry = "Item_Name" = 'John''s Cabin' 

and if that doesn't work:

qry = """ "Item_Name" = 'John''s Cabin' """
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:00:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selecting-from-a-fgdb-where-the-item-has/m-p/52083#M4135</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-10T22:00:48Z</dc:date>
    </item>
    <item>
      <title>Re: selecting from a FGDB where the item has apostrophes</title>
      <link>https://community.esri.com/t5/python-questions/selecting-from-a-fgdb-where-the-item-has/m-p/52084#M4136</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;No that isn't it. The problem isn't in the ITEM NAME but rather in the value "John's Cabin"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;because ArcMap is expecting single quotes to be around the value - but in this case the apostrophe messes that all up. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;So far, using the double quotes in python doesn't seem to work nor does using the escape character \. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;You need to enclose the field (Item_Name) in double quotes, the string value in single quotes (John's Cabin), definately escape the apostrophe with another one (John''s Cabin), and possibly wrap the whole thing in escaped double quotes. See &lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Select_Layer_By_Attribute/001700000071000000/" rel="nofollow noopener noreferrer" target="_blank"&gt;this page &lt;/A&gt;(Chicago examples), and try it yourself in the Select Layer By Attribute toolbox tool expression builder. I would try:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;qry = "Item_Name" = 'John''s Cabin' 

and if that doesn't work:

qry = """ "Item_Name" = 'John''s Cabin' """
&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:00:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selecting-from-a-fgdb-where-the-item-has/m-p/52084#M4136</guid>
      <dc:creator>JoshuaChan</dc:creator>
      <dc:date>2021-12-10T22:00:51Z</dc:date>
    </item>
    <item>
      <title>Re: selecting from a FGDB where the item has apostrophes</title>
      <link>https://community.esri.com/t5/python-questions/selecting-from-a-fgdb-where-the-item-has/m-p/52085#M4137</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;No that isn't it. The problem isn't in the ITEM NAME but rather in the value "John's Cabin"&lt;BR /&gt;because ArcMap is expecting single quotes to be around the value - but in this case the apostrophe messes that all up. &lt;BR /&gt;So far, using the double quotes in python doesn't seem to work nor does using the escape character \.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I t didn't work because there was an error in the first line suggested.&amp;nbsp; It should have been:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;qry = """Item_Name"" = 'John''s Cabin' &lt;STRONG&gt;"&lt;/STRONG&gt;&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The two single quotes together are SQL specific, not Python specific and that is how you include a single quote in an SQL query.&amp;nbsp; (Done it many times with an FGDB).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Feb 2012 21:02:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selecting-from-a-fgdb-where-the-item-has/m-p/52085#M4137</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2012-02-10T21:02:32Z</dc:date>
    </item>
    <item>
      <title>Re: selecting from a FGDB where the item has apostrophes</title>
      <link>https://community.esri.com/t5/python-questions/selecting-from-a-fgdb-where-the-item-has/m-p/52086#M4138</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;OK, so here's my example, with the real field name and value: &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[HTML]arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", """CONTACT_ORG"" = 'Lil''wat First Nation'")[/HTML]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I got an error in PythonWin compiler saying: &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;Failed to check - syntax error - EOF while scanning triple-quoted string literal&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;did I put one too many double quotes? &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcMap will bomb if it sees this : "ITEM" = 'Lil''wat First Nation'&amp;nbsp; because I think it stops at 'Lil' &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I t didn't work because there was an error in the first line suggested.&amp;nbsp; It should have been:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;qry = """Item_Name"" = 'John''s Cabin' &lt;STRONG&gt;"&lt;/STRONG&gt;&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;The two single quotes together are SQL specific, not Python specific and that is how you include a single quote in an SQL query.&amp;nbsp; (Done it many times with an FGDB).&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Feb 2012 22:05:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selecting-from-a-fgdb-where-the-item-has/m-p/52086#M4138</guid>
      <dc:creator>JoshuaChan</dc:creator>
      <dc:date>2012-02-10T22:05:57Z</dc:date>
    </item>
    <item>
      <title>Re: selecting from a FGDB where the item has apostrophes</title>
      <link>https://community.esri.com/t5/python-questions/selecting-from-a-fgdb-where-the-item-has/m-p/52087#M4139</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;OK. my mistake, human error caused most of the problems. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In case anyone's interested this code works: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# fixing FN name if apostrophe exists
if string.find(fn_name, "'") &amp;lt;&amp;gt; -1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'fixing apostrophe in', fn_name, 'to be queryable.'
&amp;nbsp;&amp;nbsp;&amp;nbsp; fn_name_qry = string.replace(fn_name, "'", "''")
&amp;nbsp;&amp;nbsp;&amp;nbsp; print fn_name_qry
else: 
&amp;nbsp;&amp;nbsp;&amp;nbsp; fn_name_qry = fn_name

cad_qry = cad_itm + ' = \'' + fn_name_qry + '\''&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;that results in an fn_name looking like&amp;nbsp; Lil''wat First Nation&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;and the double-single quotes is interpreted OK by ArcMap. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;thanks everyone.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:00:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selecting-from-a-fgdb-where-the-item-has/m-p/52087#M4139</guid>
      <dc:creator>JoshuaChan</dc:creator>
      <dc:date>2021-12-10T22:00:53Z</dc:date>
    </item>
    <item>
      <title>Re: selecting from a FGDB where the item has apostrophes</title>
      <link>https://community.esri.com/t5/python-questions/selecting-from-a-fgdb-where-the-item-has/m-p/1120309#M63063</link>
      <description>&lt;P&gt;Issue is that SQL query syntax needs to escape single quote, which is done by using two single quotes:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;' (in John's) needs to become '' in SQL syntax&lt;/P&gt;&lt;P&gt;I think easiest way is to split out the search term into a variable:&lt;/P&gt;&lt;P&gt;qry_item_value = "John's Cabin"&lt;/P&gt;&lt;P&gt;qry_item_value_fixed = qry_item_value.replace("'", "''")&lt;/P&gt;&lt;P&gt;qry = "Item_Name = '%s'" % qry_item_value_fixed&lt;/P&gt;</description>
      <pubDate>Thu, 25 Nov 2021 14:04:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/selecting-from-a-fgdb-where-the-item-has/m-p/1120309#M63063</guid>
      <dc:creator>DavidTillberg_community</dc:creator>
      <dc:date>2021-11-25T14:04:38Z</dc:date>
    </item>
  </channel>
</rss>

