<?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: Port Old Python to python 3 expression help in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1219982#M65782</link>
    <description>&lt;P&gt;So, I have changed it to this and still get an invalid expression error.&amp;nbsp;&lt;/P&gt;&lt;P&gt;expression = "str(!Num!)[4:7] not between '" + dateStart + "' and '" + dateEnd + "'"&lt;/P&gt;&lt;P&gt;arcpy.management.SelectLayerByAttribute("workLyr", "NEW_SELECTION", expression)&lt;/P&gt;</description>
    <pubDate>Fri, 07 Oct 2022 18:17:58 GMT</pubDate>
    <dc:creator>RaymondSeth_III1</dc:creator>
    <dc:date>2022-10-07T18:17:58Z</dc:date>
    <item>
      <title>Port Old Python to python 3 expression help</title>
      <link>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1219926#M65775</link>
      <description>&lt;P&gt;I am trying to port this code to python 3 and am having a terrible time trying to recreate this expression. I am really new to python but more specifically these expressions. the varible dateStart and dateEnd are int as well as Num in the table being a LONG&lt;/P&gt;&lt;P&gt;expression = "mid(trim(str([Num])), 5, 4) not between '" + dateStart + "' and '" + dateEnd + "'"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;arcpy.management.SelectLayerByAttribute("workLyr", "NEW_SELECTION", expression)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2022 17:33:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1219926#M65775</guid>
      <dc:creator>RaymondSeth_III1</dc:creator>
      <dc:date>2022-10-07T17:33:38Z</dc:date>
    </item>
    <item>
      <title>Re: Port Old Python to python 3 expression help</title>
      <link>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1219963#M65777</link>
      <description>&lt;P&gt;So mid(string, 5, 4) meaning give me the substring beginning at character 5 and 4 chars long, right?&lt;/P&gt;&lt;P&gt;In python it would be:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;str(!FieldName!)[5:8]&lt;/LI-CODE&gt;&lt;P&gt;or.. give me a string starting at position 5 and ending at position 8&lt;BR /&gt;so it returns characters at positions 5,6,7,8&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2022 17:06:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1219963#M65777</guid>
      <dc:creator>LeanneRodriguez</dc:creator>
      <dc:date>2022-10-07T17:06:53Z</dc:date>
    </item>
    <item>
      <title>Re: Port Old Python to python 3 expression help</title>
      <link>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1219967#M65778</link>
      <description>&lt;P&gt;That may help with part of it, but I am not sure I am formatting the entire expression correctly as I keep getting invalid expression error. The quotes and single quotes are really confusing.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2022 17:19:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1219967#M65778</guid>
      <dc:creator>RaymondSeth_III1</dc:creator>
      <dc:date>2022-10-07T17:19:48Z</dc:date>
    </item>
    <item>
      <title>Re: Port Old Python to python 3 expression help</title>
      <link>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1219972#M65780</link>
      <description>&lt;P&gt;Is the expression variable correct?&amp;nbsp; You set the expression with the variable named "expression1" but the function is taking a variable named "expression"&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2022 17:31:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1219972#M65780</guid>
      <dc:creator>Kara_Shindle</dc:creator>
      <dc:date>2022-10-07T17:31:20Z</dc:date>
    </item>
    <item>
      <title>Re: Port Old Python to python 3 expression help</title>
      <link>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1219973#M65781</link>
      <description>&lt;P&gt;yes sorry that was a copy and paste error as it appears a couple of times in the code but it matches in the code.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2022 17:33:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1219973#M65781</guid>
      <dc:creator>RaymondSeth_III1</dc:creator>
      <dc:date>2022-10-07T17:33:22Z</dc:date>
    </item>
    <item>
      <title>Re: Port Old Python to python 3 expression help</title>
      <link>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1219982#M65782</link>
      <description>&lt;P&gt;So, I have changed it to this and still get an invalid expression error.&amp;nbsp;&lt;/P&gt;&lt;P&gt;expression = "str(!Num!)[4:7] not between '" + dateStart + "' and '" + dateEnd + "'"&lt;/P&gt;&lt;P&gt;arcpy.management.SelectLayerByAttribute("workLyr", "NEW_SELECTION", expression)&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2022 18:17:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1219982#M65782</guid>
      <dc:creator>RaymondSeth_III1</dc:creator>
      <dc:date>2022-10-07T18:17:58Z</dc:date>
    </item>
    <item>
      <title>Re: Port Old Python to python 3 expression help</title>
      <link>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1220018#M65784</link>
      <description>&lt;P&gt;Python slicing is start to stop, not start through stop, so [5:8] returns only 3 characters and not 4 characters.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2022 19:20:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1220018#M65784</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2022-10-07T19:20:32Z</dc:date>
    </item>
    <item>
      <title>Re: Port Old Python to python 3 expression help</title>
      <link>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1220021#M65785</link>
      <description>&lt;P&gt;When I try just building your expression I get an error before I even get to the arcpy tool:&amp;nbsp; (The specific values I used don't matter for test building expression, just the data types matter, and I had to change !Num! to just Num for testing)&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;&amp;gt;&amp;gt;&amp;gt; Num = 123456
&amp;gt;&amp;gt;&amp;gt; dateStart = 1
&amp;gt;&amp;gt;&amp;gt; dateEnd = 2
&amp;gt;&amp;gt;&amp;gt; expression = "str(Num)[4:7] not between '" + dateStart + "' and '" + dateEnd + "'"
Traceback (most recent call last):
  File "&amp;lt;stdin&amp;gt;", line 1, in &amp;lt;module&amp;gt;
TypeError: can only concatenate str (not "int") to str
&amp;gt;&amp;gt;&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Are you sure you are pasting your exact code?&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2022 19:30:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1220021#M65785</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2022-10-07T19:30:26Z</dc:date>
    </item>
    <item>
      <title>Re: Port Old Python to python 3 expression help</title>
      <link>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1220024#M65787</link>
      <description>&lt;P&gt;If you are using ArcGIS Pro, I suggest you use literal strings or f-strings:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;expression = f"{str(Num)[4:7]} not between {dateStart} and {dateEnd}"&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 07 Oct 2022 19:33:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1220024#M65787</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2022-10-07T19:33:21Z</dc:date>
    </item>
    <item>
      <title>Re: Port Old Python to python 3 expression help</title>
      <link>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1220029#M65788</link>
      <description>&lt;P&gt;This was the original code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;expression = "mid(trim(str([Num])), 5, 4) not between '" + dateStart + "' and '" + dateEnd + "'"&lt;/P&gt;&lt;P&gt;arcpy.management.SelectLayerByAttribute("workLyr", "NEW_SELECTION", expression)&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2022 19:56:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1220029#M65788</guid>
      <dc:creator>RaymondSeth_III1</dc:creator>
      <dc:date>2022-10-07T19:56:46Z</dc:date>
    </item>
    <item>
      <title>Re: Port Old Python to python 3 expression help</title>
      <link>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1220030#M65789</link>
      <description>&lt;P&gt;how would I write that if Num is a table attribute?&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2022 19:59:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1220030#M65789</guid>
      <dc:creator>RaymondSeth_III1</dc:creator>
      <dc:date>2022-10-07T19:59:23Z</dc:date>
    </item>
    <item>
      <title>Re: Port Old Python to python 3 expression help</title>
      <link>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1220031#M65790</link>
      <description>&lt;P&gt;So you are trying to convert old VBA code to python code to genereate an SQL expression to feed into the select by attributes tool.&lt;/P&gt;&lt;P&gt;You say they are int/long fields and not date.&lt;/P&gt;&lt;P&gt;Do the date fields have just day, just&amp;nbsp; month, days since 1/1/1901, etc.&amp;nbsp; since they are int, they must be a number and can't resemble "10/7/2022".&amp;nbsp; Also, what is the number field?&amp;nbsp; How many digits, etc.&lt;/P&gt;&lt;P&gt;Would be easier to troubleshoot if we have examples of what the data looks like rather than trying to guess what you are after.&lt;/P&gt;&lt;P&gt;Would also help if we knew "where" you are trying to do this.&amp;nbsp; Script tool, stand alone python, python in Pro, Notebook, etc.&lt;/P&gt;&lt;P&gt;R_&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2022 20:00:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1220031#M65790</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2022-10-07T20:00:27Z</dc:date>
    </item>
    <item>
      <title>Re: Port Old Python to python 3 expression help</title>
      <link>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1220037#M65793</link>
      <description>&lt;P&gt;It's old python code that works as is in ArcMap 10 as a script tool and I am trying to port it to work in pro. I have tried both running it as a script tool and working with just those lines of code in a notebook within the pro-environment for troubleshooting and I am getting the same errors doing it either way. The Num is a LONG datatype attribute in a table within a feature class that will have a number like 20221007 that represents a date, the start and end date are basically just 4 digit numbers from user input that would be in the format of 1007 basically an integer that contains the month and day. So the first part of that expression is trying to strip out just the month and day (last 4 digits) so it can be compared to see if it falls outside the range given by the user.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2022 20:17:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1220037#M65793</guid>
      <dc:creator>RaymondSeth_III1</dc:creator>
      <dc:date>2022-10-07T20:17:25Z</dc:date>
    </item>
    <item>
      <title>Re: Port Old Python to python 3 expression help</title>
      <link>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1220087#M65797</link>
      <description>&lt;P&gt;Not sure how it was done in ArcMap, but don't really think this is possible.&lt;/P&gt;&lt;P&gt;from &lt;A href="https://pro.arcgis.com/en/pro-app/2.9/help/mapping/navigation/sql-reference-for-elements-used-in-query-expressions.htm#GUID-68D21843-5274-4AF4-B7F3-165892232A43" target="_self"&gt;documentation&lt;/A&gt;:&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;Here is a basic form of a SQL expression WHERE clause:&lt;/P&gt;&lt;P&gt;&amp;lt;Field_name&amp;gt; &amp;lt;Operator&amp;gt; &amp;lt;Value or String&amp;gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;So, not sure how having a value for the &amp;lt;Field_name&amp;gt; would work as the expression evaluates row by row comaring a field value to some expression.&lt;/P&gt;&lt;P&gt;However, if you were to make a new column in the data, say Num2 and calculate that to:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;Num2 = int(str(Num)[4:])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That will populate the Num2 field with the last 4 digits of the Num value:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;&amp;gt;&amp;gt;&amp;gt; Num = 20221007
&amp;gt;&amp;gt;&amp;gt; Num2 = int(str(Num)[4:])
&amp;gt;&amp;gt;&amp;gt; Num2
1007&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You would then have a "Field" with the value you want to compare with the expression.&amp;nbsp; Something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;expression = f"Num2 BETWEEN {startDate} And {endDate}"
arcpy.management.SelectLayerByAttribute("workLyr", "NEW_SELECTION", expression)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is working for me.&lt;/P&gt;&lt;P&gt;Suspect you could use cursors to append the matching OID's to a list and select by that.&lt;/P&gt;&lt;P&gt;Interesting to see if you find a solution as I'm not aware of how to use a variable for field input.&lt;/P&gt;&lt;P&gt;R_&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2022 23:57:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1220087#M65797</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2022-10-07T23:57:03Z</dc:date>
    </item>
    <item>
      <title>Re: Port Old Python to python 3 expression help</title>
      <link>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1221241#M65835</link>
      <description>&lt;P&gt;I am going to work on this approach I have been out of the office a couple days I will report back thanks for the for the recommendations.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2022 19:03:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/port-old-python-to-python-3-expression-help/m-p/1221241#M65835</guid>
      <dc:creator>RaymondSeth_III1</dc:creator>
      <dc:date>2022-10-12T19:03:18Z</dc:date>
    </item>
  </channel>
</rss>

