<?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: Extracting number w/ specialcharacter from random position in string field in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/extracting-number-w-specialcharacter-from-random/m-p/1019293#M59612</link>
    <description>&lt;P&gt;The pattern does seem structured and simple enough that you might not need regular expressions, but I like regular expressions like &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/363906"&gt;@jcarlson&lt;/a&gt;.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;&amp;gt;&amp;gt;&amp;gt; import re
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; rows = [
...     r"V 40 %, B112 60 %",
...     r"V 30 %, B11 30 %, K 40 %",
...     r"S 132 90 %, R113-GB00BK 10 %"
... ]
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; pct_pttn = re.compile("(\d*) %")
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; # populuate column 1
&amp;gt;&amp;gt;&amp;gt; for s in rows:
...     pct_pttn.sub("", s).strip()
...
'V , B112'
'V , B11 , K'
'S 132 , R113-GB00BK'
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; # populate column 2
&amp;gt;&amp;gt;&amp;gt; for s in rows:
...     ", ".join(pct_pttn.findall(s))
...
'40, 60'
'30, 30, 40'
'90, 10'
&amp;gt;&amp;gt;&amp;gt; &lt;/LI-CODE&gt;</description>
    <pubDate>Fri, 22 Jan 2021 21:48:22 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2021-01-22T21:48:22Z</dc:date>
    <item>
      <title>Extracting number w/ specialcharacter from random position in string field</title>
      <link>https://community.esri.com/t5/python-questions/extracting-number-w-specialcharacter-from-random/m-p/1019065#M59596</link>
      <description>&lt;P&gt;Hi all.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am struggling with extracting a number in combination with a percentage sign (e.g 40 %, 60 %; 30 %, 30 % and 40 % and so on)&amp;nbsp; from a field. The issue is that there is no regular position of said number - see example below - and that there are numbers which I dont want to extract.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas on how to extract the number-character combination? Thanks for any suggestions&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example fields:&lt;/P&gt;&lt;P&gt;V 40 %, B112 60 %&lt;/P&gt;&lt;P&gt;V 30 %, B11 30 %, K 40 %&lt;/P&gt;&lt;P&gt;S 132 90 %, R113-GB00BK 10 %&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2021 12:23:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extracting-number-w-specialcharacter-from-random/m-p/1019065#M59596</guid>
      <dc:creator>SimonLange</dc:creator>
      <dc:date>2021-01-22T12:23:08Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting number w/ specialcharacter from random position in string field</title>
      <link>https://community.esri.com/t5/python-questions/extracting-number-w-specialcharacter-from-random/m-p/1019069#M59597</link>
      <description>&lt;P&gt;Which number do you want to extract in those examples?&amp;nbsp; Can you give examples along with the intended result?&amp;nbsp; There may be no regular position, but is there a regular format - do you want the numbers preceding the % sign?&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2021 12:05:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extracting-number-w-specialcharacter-from-random/m-p/1019069#M59597</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-01-22T12:05:17Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting number w/ specialcharacter from random position in string field</title>
      <link>https://community.esri.com/t5/python-questions/extracting-number-w-specialcharacter-from-random/m-p/1019071#M59598</link>
      <description>&lt;P&gt;Hi David - thanks for your response. I specified my question to which numbers I want to extract!&lt;/P&gt;&lt;P&gt;The codes are always separated by a comma - the codes themselfes vary though&lt;/P&gt;&lt;P&gt;Intended result would be a column with&lt;/P&gt;&lt;P&gt;V, B112&lt;/P&gt;&lt;P&gt;V, B11, K&lt;/P&gt;&lt;P&gt;S132, R113-GB00BK&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and one with&lt;/P&gt;&lt;P&gt;40, 60&lt;/P&gt;&lt;P&gt;30, 30, 40&lt;/P&gt;&lt;P&gt;90, 10&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2021 12:28:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extracting-number-w-specialcharacter-from-random/m-p/1019071#M59598</guid>
      <dc:creator>SimonLange</dc:creator>
      <dc:date>2021-01-22T12:28:55Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting number w/ specialcharacter from random position in string field</title>
      <link>https://community.esri.com/t5/python-questions/extracting-number-w-specialcharacter-from-random/m-p/1019072#M59599</link>
      <description>&lt;P&gt;This will get you thinkin about what you want.&lt;/P&gt;&lt;P&gt;Currently it returns a list so you have to decide whether you want a particular value from the list (eg first, last etc) or whether you want to concatenate the values together (eg ", ".join([i for i in ... )&lt;/P&gt;&lt;LI-CODE lang="python"&gt;s = "S 132 90 %, R113-GB00BK 10 %"
[i.strip().split(" ")[-1] for i in s.split("%") if i]
['90', '10']

s0 = "V 30 %, B11 30 %, K 40 %"
[i.strip().split(" ")[-1] for i in s0.split("%") if i]
['30', '30', '40']

s1 = "V 40 %, B112 60 %"
[i.strip().split(" ")[-1] for i in s1.split("%") if i]
['40', '60']
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2021 12:37:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extracting-number-w-specialcharacter-from-random/m-p/1019072#M59599</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-01-22T12:37:00Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting number w/ specialcharacter from random position in string field</title>
      <link>https://community.esri.com/t5/python-questions/extracting-number-w-specialcharacter-from-random/m-p/1019073#M59600</link>
      <description>&lt;P&gt;", ".join([i.strip().split(" ")[-1] for i in s0.split("%") if i])&lt;BR /&gt;'30, 30, 40'&lt;/P&gt;&lt;P&gt;like so?&amp;nbsp; See my post&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2021 12:50:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extracting-number-w-specialcharacter-from-random/m-p/1019073#M59600</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-01-22T12:50:12Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting number w/ specialcharacter from random position in string field</title>
      <link>https://community.esri.com/t5/python-questions/extracting-number-w-specialcharacter-from-random/m-p/1019107#M59604</link>
      <description>&lt;P&gt;If the data are consistently &lt;EM&gt;formatted&lt;/EM&gt;, you might consider looking at &lt;A href="https://docs.python.org/3/library/re.html" target="_self"&gt;regular expressions&lt;/A&gt; to accomplish this.&lt;/P&gt;&lt;P&gt;Here's my code:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import re

values = [
    'V 40 %, B112 60 %',
    'V 30 %, B11 30 %, K 40 %',
    'S 132 90 %, R113-GB00BK 10 %'
]

# Regex patterns
other_patt = re.compile('\S+?(?=\s[0-9]+\s%)')
percent_patt = re.compile('[0-9]+(?=\s%)')

for value in values:
    print([re.findall(other_patt, value), re.findall(percent_patt, value)])&lt;/LI-CODE&gt;&lt;P&gt;And here's what it returns:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[['V', 'B112'], ['40', '60']]
[['V', 'B11', 'K'], ['30', '30', '40']]
[['132', 'R113-GB00BK'], ['90', '10']]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Breaking down the regex patterns:&lt;/P&gt;&lt;DIV&gt;&lt;UL&gt;&lt;LI&gt;\S+?(?=\s[0-9]+\s%)&lt;UL&gt;&lt;LI&gt;\S+?&lt;UL&gt;&lt;LI&gt;\S+ matches one or more non-whitespace characters.&lt;/LI&gt;&lt;LI&gt;The '?' makes it non-greedy, meaning it will match as little as possible, so that we don't inadvertently grab more than one value&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;LI&gt;(?=...) Indicates a lookahead expression, meaning it specifically looks for strings which are followed by the value in the '...', but does not include that value in the returned match&lt;/LI&gt;&lt;LI&gt;\s looks for a whitespace character&lt;/LI&gt;&lt;LI&gt;[0-9]+ Looks for one or more consecutive numeric characters.&lt;/LI&gt;&lt;LI&gt;% Just a literal '%' character!&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;LI&gt;[0-9]+(?=\s%)&lt;UL&gt;&lt;LI&gt;Again, [0-9]+ is looking for one or more consecutive numeric characters.&lt;/LI&gt;&lt;LI&gt;(?=\s$) A simpler lookahead expression, this time looking only for those numeric characters followed by a single whitespace character and a percent sign&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Regex is quite useful for extracting text, and is a module well worth digging into.&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 22 Jan 2021 15:03:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extracting-number-w-specialcharacter-from-random/m-p/1019107#M59604</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2021-01-22T15:03:56Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting number w/ specialcharacter from random position in string field</title>
      <link>https://community.esri.com/t5/python-questions/extracting-number-w-specialcharacter-from-random/m-p/1019293#M59612</link>
      <description>&lt;P&gt;The pattern does seem structured and simple enough that you might not need regular expressions, but I like regular expressions like &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/363906"&gt;@jcarlson&lt;/a&gt;.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;&amp;gt;&amp;gt;&amp;gt; import re
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; rows = [
...     r"V 40 %, B112 60 %",
...     r"V 30 %, B11 30 %, K 40 %",
...     r"S 132 90 %, R113-GB00BK 10 %"
... ]
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; pct_pttn = re.compile("(\d*) %")
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; # populuate column 1
&amp;gt;&amp;gt;&amp;gt; for s in rows:
...     pct_pttn.sub("", s).strip()
...
'V , B112'
'V , B11 , K'
'S 132 , R113-GB00BK'
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; # populate column 2
&amp;gt;&amp;gt;&amp;gt; for s in rows:
...     ", ".join(pct_pttn.findall(s))
...
'40, 60'
'30, 30, 40'
'90, 10'
&amp;gt;&amp;gt;&amp;gt; &lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 22 Jan 2021 21:48:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extracting-number-w-specialcharacter-from-random/m-p/1019293#M59612</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-01-22T21:48:22Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting number w/ specialcharacter from random position in string field</title>
      <link>https://community.esri.com/t5/python-questions/extracting-number-w-specialcharacter-from-random/m-p/1019586#M59618</link>
      <description>&lt;P&gt;Thanks very much - that helped a lot!&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jan 2021 12:43:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extracting-number-w-specialcharacter-from-random/m-p/1019586#M59618</guid>
      <dc:creator>SimonLange</dc:creator>
      <dc:date>2021-01-25T12:43:03Z</dc:date>
    </item>
  </channel>
</rss>

