<?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 truncate trailing digits from string without knowing number of trailing digits? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/truncate-trailing-digits-from-string-without/m-p/403038#M31722</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;What's the most elegant way to truncate trailing digits from a string without knowing beforehand how many trailing digits you will encounter.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;In particular, I want to be careful not to truncate digits contained within a given string's basename or those that a prefixed, only the suffix values that are digits.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;"7-Eleven" : I don't want to truncate anything.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"7-Eleven 2" : I want to truncate the '2'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"7-Eleven 2364" : I want to truncate the '2364'.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That last part if a bit of an exaggeration, I have exceptions in place to keep things from getting crazy, but there could be instances where I don't know how many digits to expect beforehand and need to truncate them from the end of the string.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thus far, I gotten to something along the lines of:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;if str(MyLayer.name[-1]).isDigit(): &amp;nbsp;&amp;nbsp;&amp;nbsp; newName = str(MyLayer.name[:-1]) elif str(MyLayer.name[-2:].isDigit(): &amp;nbsp;&amp;nbsp;&amp;nbsp; newName = str(MyLayer.name[:-2])&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You can see how this could go on and on...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm looking for some sort of dynamic method to determine the number of trailing characters I need to drop in order to remove all of the trailing digits from the Layer Name.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 27 Jan 2014 15:27:38 GMT</pubDate>
    <dc:creator>JohnDye</dc:creator>
    <dc:date>2014-01-27T15:27:38Z</dc:date>
    <item>
      <title>truncate trailing digits from string without knowing number of trailing digits?</title>
      <link>https://community.esri.com/t5/python-questions/truncate-trailing-digits-from-string-without/m-p/403038#M31722</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;What's the most elegant way to truncate trailing digits from a string without knowing beforehand how many trailing digits you will encounter.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;In particular, I want to be careful not to truncate digits contained within a given string's basename or those that a prefixed, only the suffix values that are digits.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;"7-Eleven" : I don't want to truncate anything.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"7-Eleven 2" : I want to truncate the '2'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"7-Eleven 2364" : I want to truncate the '2364'.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That last part if a bit of an exaggeration, I have exceptions in place to keep things from getting crazy, but there could be instances where I don't know how many digits to expect beforehand and need to truncate them from the end of the string.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thus far, I gotten to something along the lines of:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;if str(MyLayer.name[-1]).isDigit(): &amp;nbsp;&amp;nbsp;&amp;nbsp; newName = str(MyLayer.name[:-1]) elif str(MyLayer.name[-2:].isDigit(): &amp;nbsp;&amp;nbsp;&amp;nbsp; newName = str(MyLayer.name[:-2])&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You can see how this could go on and on...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm looking for some sort of dynamic method to determine the number of trailing characters I need to drop in order to remove all of the trailing digits from the Layer Name.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Jan 2014 15:27:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/truncate-trailing-digits-from-string-without/m-p/403038#M31722</guid>
      <dc:creator>JohnDye</dc:creator>
      <dc:date>2014-01-27T15:27:38Z</dc:date>
    </item>
    <item>
      <title>Re: truncate trailing digits from string without knowing number of trailing digits?</title>
      <link>https://community.esri.com/t5/python-questions/truncate-trailing-digits-from-string-without/m-p/403039#M31723</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello John,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;There are probably 1000 good ways to do this. One way is to use regular expressions, but since RegEx has complicated syntax (in my option), I would do something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;s='7-Eleven 2364' while s[-1:].isdigit() or s[-1:]==" ": &amp;nbsp;&amp;nbsp;&amp;nbsp; s=s[:-1]&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt;Note it also gets rid of any tailing spaces.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Good luck!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Jan 2014 16:01:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/truncate-trailing-digits-from-string-without/m-p/403039#M31723</guid>
      <dc:creator>JoshuaChisholm</dc:creator>
      <dc:date>2014-01-27T16:01:11Z</dc:date>
    </item>
    <item>
      <title>Re: truncate trailing digits from string without knowing number of trailing digits?</title>
      <link>https://community.esri.com/t5/python-questions/truncate-trailing-digits-from-string-without/m-p/403040#M31724</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Hello John,&lt;BR /&gt;&lt;BR /&gt;There are probably 1000 good ways to do this. One way is to use regular expressions, but since RegEx has complicated syntax (in my option), I would do something like this:&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
s='7-Eleven 2364'
while s[-1:].isdigit() or s[-1:]==" ":
&amp;nbsp;&amp;nbsp;&amp;nbsp; s=s[:-1]
&lt;/PRE&gt;&lt;BR /&gt;Note it also gets rid of any tailing spaces.&lt;BR /&gt;&lt;BR /&gt;Good luck!&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;You know, it's so rare for me to encounter a condition where a while-loop is warranted that I didn't even think to use it. Suppose that's a testament to my infantile coding abilities, lol. Thanks Joshua!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:22:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/truncate-trailing-digits-from-string-without/m-p/403040#M31724</guid>
      <dc:creator>JohnDye</dc:creator>
      <dc:date>2021-12-11T18:22:16Z</dc:date>
    </item>
    <item>
      <title>Re: truncate trailing digits from string without knowing number of trailing digits?</title>
      <link>https://community.esri.com/t5/python-questions/truncate-trailing-digits-from-string-without/m-p/403041#M31725</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Just for reference, the regular expression you'd use and how you'd use it:&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;re.sub(" \d+$", "", s)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Where the &lt;/SPAN&gt;&lt;SPAN style="font-family:Courier New;"&gt;" \d+$"&lt;/SPAN&gt;&lt;SPAN&gt; means: one space, a digit(\d) one or more times(+), followed by the end of the string($).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Jan 2014 16:30:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/truncate-trailing-digits-from-string-without/m-p/403041#M31725</guid>
      <dc:creator>JasonScheirer</dc:creator>
      <dc:date>2014-01-27T16:30:07Z</dc:date>
    </item>
    <item>
      <title>Re: truncate trailing digits from string without knowing number of trailing digits?</title>
      <link>https://community.esri.com/t5/python-questions/truncate-trailing-digits-from-string-without/m-p/403042#M31726</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You can do a one-liner like the following:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;test = "7 Eleven 13435423"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;test_trunc =&amp;nbsp; " ".join(test.split()[:-1]) if test[-1].isdigit() else test&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Although regex looks cleaner, I personally haven't learned it yet either.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Jan 2014 16:42:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/truncate-trailing-digits-from-string-without/m-p/403042#M31726</guid>
      <dc:creator>ClintDow</dc:creator>
      <dc:date>2014-01-27T16:42:19Z</dc:date>
    </item>
    <item>
      <title>Re: truncate trailing digits from string without knowing number of trailing digits?</title>
      <link>https://community.esri.com/t5/python-questions/truncate-trailing-digits-from-string-without/m-p/403043#M31727</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;You know, it's so rare for me to encounter a condition where a while-loop is warranted that I didn't even think to use it. Suppose that's a testament to my infantile coding abilities, lol. Thanks Joshua!&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;So little bit of a complication. I keep ending up in an infinite loop using the while-loop, but I'm not sure why.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's the code I'm using:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
itemName = "Starbucks 1234_"
while str(itemName[-1:]).isdigit() or str(itemName[-1:]) == "_" or str(itemName[-1:]) == " ":
&amp;nbsp;&amp;nbsp;&amp;nbsp; itemDisplayName = itemName[:-1]
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I just want to populate the result in 'itemDisplayName' and not neccesarily alter 'itemName' itself.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;If I use:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
itemName = "Starbucks 1234_"
while str(itemName[-1:]).isdigit() or str(itemName[-1:]) == "_" or str(itemName[-1:]) == " ":
&amp;nbsp;&amp;nbsp;&amp;nbsp; itemName = itemName[:-1]
&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;Then everything goes smoothly. But it's when I try to populate the result in a different variable that it seems to run away into the abyss. Thoughts anyone??&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;UPDATE:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;What I ended up doing in order to solve the above but continue using the while-loop but avoid the infinite loop was to just prepopulate 'itemDisplayName' with the contents of 'itemName' and THEN run the while-loop against the 'itemDisplayName' giving me the result in the variable I wanted.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
itemName = "Starbucks 1234_"
itemDisplayName = itemName
while str(itemDisplayName[-1:]).isdigit() or str(itemDisplayName[-1:]) == "_" or str(itemDisplayName[-1:]) == " ":
&amp;nbsp;&amp;nbsp;&amp;nbsp; itemDisplayName = itemDisplayName[:-1]
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:22:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/truncate-trailing-digits-from-string-without/m-p/403043#M31727</guid>
      <dc:creator>JohnDye</dc:creator>
      <dc:date>2021-12-11T18:22:19Z</dc:date>
    </item>
    <item>
      <title>Re: truncate trailing digits from string without knowing number of trailing digits?</title>
      <link>https://community.esri.com/t5/python-questions/truncate-trailing-digits-from-string-without/m-p/403044#M31728</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;In that case the loop is checking the last char in itemName repeatedly since you're only checking the last value of itemName while not removing it. Therefore the loops gets "_" over and over, however if you update itemName inside the loop it removes the last character one by one. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;if you wanted to use that solution you would have to do something like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;i = 1
while not itemName[-i].isalpha():
&amp;nbsp;&amp;nbsp;&amp;nbsp; itemDisplayName = itemName[:-i]
&amp;nbsp;&amp;nbsp;&amp;nbsp; i+=1&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Then itemDisplayName should work as expected.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:22:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/truncate-trailing-digits-from-string-without/m-p/403044#M31728</guid>
      <dc:creator>ClintDow</dc:creator>
      <dc:date>2021-12-11T18:22:22Z</dc:date>
    </item>
    <item>
      <title>Re: truncate trailing digits from string without knowing number of trailing digits?</title>
      <link>https://community.esri.com/t5/python-questions/truncate-trailing-digits-from-string-without/m-p/403045#M31729</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;In that case the loop is checking the last char in itemName repeatedly since you're only checking the last value of itemName while not removing it. Therefore the loops gets "_" over and over, however if you update itemName inside the loop it removes the last character one by one. &lt;BR /&gt;&lt;BR /&gt;if you wanted to use that solution you would have to do something like:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;i = 1
while not itemName[-i].isalpha():
&amp;nbsp;&amp;nbsp;&amp;nbsp; itemDisplayName = itemName[:-i]
&amp;nbsp;&amp;nbsp;&amp;nbsp; i+=1&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;Then itemDisplayName should work as expected.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Oh that's nifty.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 18:22:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/truncate-trailing-digits-from-string-without/m-p/403045#M31729</guid>
      <dc:creator>JohnDye</dc:creator>
      <dc:date>2021-12-11T18:22:24Z</dc:date>
    </item>
  </channel>
</rss>

