<?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 Problems single quotes and variables used in Where Clause in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/problems-single-quotes-and-variables-used-in-where/m-p/61937#M4940</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I did not know where else to put this.&amp;nbsp; I have a python list of string variables from an Arc Table I made, some of which contain single quotes in the middle of them.&amp;nbsp; I am trying to have a for loop iterate through use these string values in the Where Clause of a few&amp;nbsp; arcpy tools.&amp;nbsp; The only thing is that in a where clause, since a string value has to to be in single quotes, the single quote in the middle of the string values is throwing an error and messing up the quotations that Arcmap is reading in the Where Clause.&amp;nbsp; Other than having to have an update cursor go through and remove all the quotes from the string values, is there any way to get Arcpy tools with where clauses to work with strings that have a single quotation mark in them?&amp;nbsp; I have already tried escaping it with \ and r', both of which didn't work.&amp;nbsp; Any ideas?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 11 Apr 2014 12:42:34 GMT</pubDate>
    <dc:creator>MattGray</dc:creator>
    <dc:date>2014-04-11T12:42:34Z</dc:date>
    <item>
      <title>Problems single quotes and variables used in Where Clause</title>
      <link>https://community.esri.com/t5/python-questions/problems-single-quotes-and-variables-used-in-where/m-p/61937#M4940</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I did not know where else to put this.&amp;nbsp; I have a python list of string variables from an Arc Table I made, some of which contain single quotes in the middle of them.&amp;nbsp; I am trying to have a for loop iterate through use these string values in the Where Clause of a few&amp;nbsp; arcpy tools.&amp;nbsp; The only thing is that in a where clause, since a string value has to to be in single quotes, the single quote in the middle of the string values is throwing an error and messing up the quotations that Arcmap is reading in the Where Clause.&amp;nbsp; Other than having to have an update cursor go through and remove all the quotes from the string values, is there any way to get Arcpy tools with where clauses to work with strings that have a single quotation mark in them?&amp;nbsp; I have already tried escaping it with \ and r', both of which didn't work.&amp;nbsp; Any ideas?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 11 Apr 2014 12:42:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problems-single-quotes-and-variables-used-in-where/m-p/61937#M4940</guid>
      <dc:creator>MattGray</dc:creator>
      <dc:date>2014-04-11T12:42:34Z</dc:date>
    </item>
    <item>
      <title>Re: Problems single quotes and variables used in Where Clause</title>
      <link>https://community.esri.com/t5/python-questions/problems-single-quotes-and-variables-used-in-where/m-p/61938#M4941</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Wow, that is tricky...I figured this out by doing the query manually in ArcMap...Looks like you have to escape the single quote with a another single quote...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So here were the values I was playing with:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
rows = arcpy.SearchCursor("Quadrants")
&amp;gt;&amp;gt;&amp;gt; qts = [r.Quotes for r in rows]
&amp;gt;&amp;gt;&amp;gt; del rows
&amp;gt;&amp;gt;&amp;gt; qts
[u"test with quotes' 2", u"test with quotes' 3", u"test with quotes' 4", u"test with quotes' 5"]
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To bypass the invalid expression error I had to replace the single quotes with 2 single quotes:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
for q in qts:
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where = '''"Quotes" = '{0}' '''.format(q.replace("'","''"))
...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("Quadrants","NEW_SELECTION", where)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This properly selected everything...That was interesting; I've never had to do that before.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:21:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problems-single-quotes-and-variables-used-in-where/m-p/61938#M4941</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-10T22:21:32Z</dc:date>
    </item>
    <item>
      <title>Re: Problems single quotes and variables used in Where Clause</title>
      <link>https://community.esri.com/t5/python-questions/problems-single-quotes-and-variables-used-in-where/m-p/61939#M4942</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the quick reply.&amp;nbsp; So far my script is working without failing, it will be interesting to see what the results are.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 11 Apr 2014 14:26:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problems-single-quotes-and-variables-used-in-where/m-p/61939#M4942</guid>
      <dc:creator>MattGray</dc:creator>
      <dc:date>2014-04-11T14:26:31Z</dc:date>
    </item>
  </channel>
</rss>

