<?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: Accounting for special characters in dynamic arcpy Search by Attribute query statement in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/accounting-for-special-characters-in-dynamic-arcpy/m-p/1097262#M45441</link>
    <description>&lt;P&gt;oh an apostrophe, that's a pain. maybe&lt;/P&gt;&lt;LI-CODE lang="python"&gt;"Subd_Name = '{}'".format(sub.replace("'", "''"))&lt;/LI-CODE&gt;</description>
    <pubDate>Fri, 10 Sep 2021 16:13:57 GMT</pubDate>
    <dc:creator>DavidPike</dc:creator>
    <dc:date>2021-09-10T16:13:57Z</dc:date>
    <item>
      <title>Accounting for special characters in dynamic arcpy Search by Attribute query statement</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/accounting-for-special-characters-in-dynamic-arcpy/m-p/1097178#M45422</link>
      <description>&lt;P&gt;I cannot be the only person to come across this problem, but I have yet to find a solution that works.&lt;/P&gt;&lt;P&gt;I have a script that counts the number of features that intersects another feature, and cycles through the file using the UpdateCursor. Everything has been going smoothly, until I came across an attribute that has special characters in it. Due to reasons, I cannot remove the apostrophes, ampersands, forward slashes, etc. from the attribute data and have to account for these in the code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;edit.startEditing(False, True)
edit.startOperation()

with arcpy.da.UpdateCursor(subdivisions, ['Subd_Name', 'LOTS', 'CON_STATUS']) as subdivUpdate:
    for each in subdivUpdate:
        if subdivUpdate[2] != "UNDERCON" or subdivUpdate[2] != "ESTBLSHD":
            sub = subdivUpdate[0]
            query1 = 'Subd_Name = \'{}\''.format(sub)
            subDiv = arcpy.management.SelectLayerByAttribute(subdivisions, 'NEW_SELECTION', query1)
            selectedParcels = arcpy.management.SelectLayerByLocation(parcels, 'HAVE_THEIR_CENTER_IN', subDiv)

            numberOfParcels = arcpy.GetCount_management(selectedParcels)
            print(subdivUpdate[0])
            each[1] = int(str(numberOfParcels))
            print(numberOfParcels)

        subdivUpdate.updateRow(each)

edit.stopOperation()
edit.stopEditing(True)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Everything runs the way it is supposed to until it comes across a record with a special character in it, and throws the error,&amp;nbsp; arcgisscripting.ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).&lt;/P&gt;&lt;P&gt;I have tried several combinations of triple quotes around the query, but they've all failed as soon as it reaches a record with special characters. If there's a better way of dynamically inserting the query value I'm all ears.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for any help!&lt;/P&gt;</description>
      <pubDate>Fri, 10 Sep 2021 13:48:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/accounting-for-special-characters-in-dynamic-arcpy/m-p/1097178#M45422</guid>
      <dc:creator>OlyPowers</dc:creator>
      <dc:date>2021-09-10T13:48:22Z</dc:date>
    </item>
    <item>
      <title>Re: Accounting for special characters in dynamic arcpy Search by Attribute query statement</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/accounting-for-special-characters-in-dynamic-arcpy/m-p/1097198#M45425</link>
      <description>&lt;P&gt;You aren't the only one; if you search on something like 'special characters' in the python space, you'll see I am your compadre in pain.&amp;nbsp; The problem with &lt;STRONG&gt;&lt;EM&gt;allowing&amp;nbsp;&lt;/EM&gt;&lt;/STRONG&gt;special characters in a database is they may be great for the those entering/creating the data but they suck for those of use who actually &lt;STRONG&gt;&lt;EM&gt;use the data.&amp;nbsp;&amp;nbsp;&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;As you've probably discovered, once you filter out for one special character, there are a dozen more that pop up.&lt;/P&gt;&lt;P&gt;Can you provide some of the data that it's choking on?&amp;nbsp; Even just in the form of a csv.&amp;nbsp; Maybe another set of eyes is all you need.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;EDITED MOMENTS LATER....&lt;/P&gt;&lt;P data-unlink="true"&gt;I just noticed in lines 9 &amp;amp; 10 you haven't used &lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/make-feature-layer.htm" target="_self"&gt;arcpy.makeFeatureLayer&lt;/A&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;prior to your selections.&amp;nbsp; I wonder if that might be you problem.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Sep 2021 14:35:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/accounting-for-special-characters-in-dynamic-arcpy/m-p/1097198#M45425</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-09-10T14:35:45Z</dc:date>
    </item>
    <item>
      <title>Re: Accounting for special characters in dynamic arcpy Search by Attribute query statement</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/accounting-for-special-characters-in-dynamic-arcpy/m-p/1097221#M45428</link>
      <description>&lt;P&gt;What about raw string formatting?&lt;/P&gt;</description>
      <pubDate>Fri, 10 Sep 2021 15:00:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/accounting-for-special-characters-in-dynamic-arcpy/m-p/1097221#M45428</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-09-10T15:00:10Z</dc:date>
    </item>
    <item>
      <title>Re: Accounting for special characters in dynamic arcpy Search by Attribute query statement</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/accounting-for-special-characters-in-dynamic-arcpy/m-p/1097223#M45429</link>
      <description>&lt;P&gt;Good idea&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/167692"&gt;@DavidPike&lt;/a&gt;&amp;nbsp;!&lt;/P&gt;</description>
      <pubDate>Fri, 10 Sep 2021 15:02:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/accounting-for-special-characters-in-dynamic-arcpy/m-p/1097223#M45429</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-09-10T15:02:16Z</dc:date>
    </item>
    <item>
      <title>Re: Accounting for special characters in dynamic arcpy Search by Attribute query statement</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/accounting-for-special-characters-in-dynamic-arcpy/m-p/1097238#M45435</link>
      <description>&lt;P&gt;I'm unsure about the FeatureLayer. I think I'll still have an issue with the tool not being able to read the query statement regardless. Some of my Problem values are:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;&lt;FONT size="3"&gt;BRAUM'S ADDITION&lt;/FONT&gt;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;&lt;FONT size="3"&gt;BROWN &amp;amp; BROWN SUBD&lt;/FONT&gt;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;&lt;FONT size="3"&gt;I-30 - 205 PLAZA 1&lt;/FONT&gt;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;&lt;FONT size="3"&gt;K.H.D. INC. SUBDIVISION&lt;/FONT&gt;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;&lt;FONT size="3"&gt;PANEX LTD ADDN (REPLAT)&lt;/FONT&gt;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;&lt;FONT size="3"&gt;SUBWAY / GATEWAY/ HEALTH FOODSTORE&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;So I've got a pretty wide range of characters to deal with, and who knows what could be added in the future.&amp;nbsp; &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/167692"&gt;@DavidPike&lt;/a&gt;&amp;nbsp;I've been playing with raw string formatting, but haven't been successful as of yet. &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Sep 2021 15:35:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/accounting-for-special-characters-in-dynamic-arcpy/m-p/1097238#M45435</guid>
      <dc:creator>OlyPowers</dc:creator>
      <dc:date>2021-09-10T15:35:46Z</dc:date>
    </item>
    <item>
      <title>Re: Accounting for special characters in dynamic arcpy Search by Attribute query statement</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/accounting-for-special-characters-in-dynamic-arcpy/m-p/1097242#M45436</link>
      <description>&lt;LI-CODE lang="python"&gt;query1 = r"Subd_Name = '{}'".format(sub)&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 10 Sep 2021 15:37:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/accounting-for-special-characters-in-dynamic-arcpy/m-p/1097242#M45436</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-09-10T15:37:33Z</dc:date>
    </item>
    <item>
      <title>Re: Accounting for special characters in dynamic arcpy Search by Attribute query statement</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/accounting-for-special-characters-in-dynamic-arcpy/m-p/1097252#M45440</link>
      <description>&lt;P&gt;It's still encountering the error. I printed the query before it runs the selection, and it's coming out as:&lt;/P&gt;&lt;P&gt;Subd_Name = 'BRAUM'S ADDITION'&lt;/P&gt;&lt;P&gt;ArcGIS Pro accounts for the apostrophe by doubling it, but I'm unsure how to do that dynamically in this situation.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Sep 2021 15:56:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/accounting-for-special-characters-in-dynamic-arcpy/m-p/1097252#M45440</guid>
      <dc:creator>OlyPowers</dc:creator>
      <dc:date>2021-09-10T15:56:46Z</dc:date>
    </item>
    <item>
      <title>Re: Accounting for special characters in dynamic arcpy Search by Attribute query statement</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/accounting-for-special-characters-in-dynamic-arcpy/m-p/1097262#M45441</link>
      <description>&lt;P&gt;oh an apostrophe, that's a pain. maybe&lt;/P&gt;&lt;LI-CODE lang="python"&gt;"Subd_Name = '{}'".format(sub.replace("'", "''"))&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 10 Sep 2021 16:13:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/accounting-for-special-characters-in-dynamic-arcpy/m-p/1097262#M45441</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-09-10T16:13:57Z</dc:date>
    </item>
    <item>
      <title>Re: Accounting for special characters in dynamic arcpy Search by Attribute query statement</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/accounting-for-special-characters-in-dynamic-arcpy/m-p/1115330#M47667</link>
      <description>&lt;P&gt;Chiming in here, though I might be misunderstanding the question.&lt;/P&gt;&lt;P&gt;I simplified OP's case to isolate the error, and created&amp;nbsp;a featureclass with the field &lt;EM&gt;Name&lt;/EM&gt; containing the subdivision names OP provided in a comment. I got the same error OP got.&lt;/P&gt;&lt;P&gt;I think the problem might just be that any &lt;FONT face="courier new,courier"&gt;'&lt;/FONT&gt; characters need to be escaped by doubling these up. I'd also suggest making use of &lt;FONT face="courier new,courier"&gt;arcpy.AddFieldDelimiters&lt;/FONT&gt;. So when I use&amp;nbsp;&lt;FONT face="courier new,courier"&gt;re.sub&lt;/FONT&gt; to add an additional &lt;FONT face="courier new,courier"&gt;'&lt;/FONT&gt; in front of any &lt;FONT face="courier new,courier"&gt;'&lt;/FONT&gt; character encountered (so that &lt;FONT face="courier new,courier"&gt;'&lt;/FONT&gt; becomes &lt;FONT face="courier new,courier"&gt;''&lt;/FONT&gt;), the query works for all OP's strings.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.UpdateCursor(fc, ["Name"]) as cursor:
    for row in cursor:
        subdiv = re.sub(r"(')", r"'\1", row[0])
        query = f"{arcpy.AddFieldDelimiters(fc, 'Name')} = '{subdiv}'"
        print(query)
        sel = arcpy.management.SelectLayerByAttribute(fc, "NEW_SELECTION", query)
        print('Selected OID(s):', arcpy.Describe(sel).FIDSet)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This prints the following to console:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Name = 'BRAUM''S ADDITION'&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;Selected OID(s): 1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;Name = 'BROWN &amp;amp; BROWN SUBD'&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;Selected OID(s): 2&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;Name = 'I-30 - 205 PLAZA 1'&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;Selected OID(s): 3&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;Name = 'K.H.D. INC. SUBDIVISION'&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;Selected OID(s): 4&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;Name = 'PANEX LTD ADDN (REPLAT)'&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;Selected OID(s): 6&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;Name = 'SUBWAY / GATEWAY/ HEALTH FOODSTORE'&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;Selected OID(s): 7&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Nov 2021 17:40:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/accounting-for-special-characters-in-dynamic-arcpy/m-p/1115330#M47667</guid>
      <dc:creator>HannesZiegler</dc:creator>
      <dc:date>2021-11-09T17:40:43Z</dc:date>
    </item>
  </channel>
</rss>

