<?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 an integer from a string field in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180865#M13886</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;from Phil's 2nd post... where a solution works 95% of the time.. perhaps find a solution for it and then determine whether the remaining 5% has unique characteristics that the first 95% doesn't share... Often a two stage solution is faster and more elegant than a one-pass run.&amp;nbsp; It would be interesting to see this type of dataset broken down on those lines.&amp;nbsp; I am sure that people dealing with geocoding all the time, can come up with examples where failure of the one-step solution occurs all the time.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 15 Jan 2016 01:35:11 GMT</pubDate>
    <dc:creator>DanPatterson_Retired</dc:creator>
    <dc:date>2016-01-15T01:35:11Z</dc:date>
    <item>
      <title>Extracting an integer from a string field</title>
      <link>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180857#M13878</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have an attribute table with a column populated with road names.&amp;nbsp; The names are in the form, "State Hwy 13", "US Hwy 1", "County Rd 444", etc.&amp;nbsp; I have created another column in the table which I would like to populate with just the numerical portions of these road names.&amp;nbsp; I thought this would be easy with the field calculator, but I am finding that to not be the case.&amp;nbsp; I would like to use Python, but if VBScript would work better, that would be ok as well.&amp;nbsp; Please keep in mind that I know very little about Python and have used Visual Basic only in Excel macros, so if you post some code, do not leave anything out thinking I will know what is missing.&amp;nbsp; I am running ArcMap 10.2&amp;nbsp; Any help will be greatly appreciated.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 23 Dec 2013 00:24:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180857#M13878</guid>
      <dc:creator>PhilFreeman</dc:creator>
      <dc:date>2013-12-23T00:24:14Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting an integer from a string field</title>
      <link>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180858#M13879</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; import string&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; a = "US Hwy 1"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; b = string.split(a," ")[-1]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; b&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;'1'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 23 Dec 2013 01:06:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180858#M13879</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2013-12-23T01:06:27Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting an integer from a string field</title>
      <link>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180859#M13880</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Dan,&amp;nbsp; I appreciate the quick reply, but I guess I didn't state the problem clearly enough.&amp;nbsp; My examples made it appear that the string always ends with the number I am trying to extract, but that is only the case about 95% of the time.&amp;nbsp; So for example, if the string reads, "State Hwy 33 W", the script returns "W".&amp;nbsp; For "State Hwy 29 Bus", it returns "Bus".&amp;nbsp; Can this script be modified to eliminate those problems?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 23 Dec 2013 02:30:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180859#M13880</guid>
      <dc:creator>PhilFreeman</dc:creator>
      <dc:date>2013-12-23T02:30:55Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting an integer from a string field</title>
      <link>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180860#M13881</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Phil,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The string.isdigit() can be used to test if it is numeric. Looping through the split list (from back) will result in the first number.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;lstRoads = ['US Hwy 1', 'State Hwy 33 W','State Hwy 29 Bus', 'some 1 other string text with a number','yet another test with a 6 number']
for r in lstRoads:
&amp;nbsp;&amp;nbsp;&amp;nbsp; lstr = r.split(' ')
&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(len(lstr)-1,-1,-1):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lstr&lt;I&gt;.isdigit():
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print " - part {0} - in string '{1}' is '{2}' and this is numeric {3}".format(i,r,lstr&lt;I&gt;,float(lstr&lt;I&gt;))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;returns:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family:courier new;"&gt; - part 2 - in string 'US Hwy 1' is '1' and this is numeric 1.0&lt;BR /&gt; - part 2 - in string 'State Hwy 33 W' is '33' and this is numeric 33.0&lt;BR /&gt; - part 2 - in string 'State Hwy 29 Bus' is '29' and this is numeric 29.0&lt;BR /&gt; - part 1 - in string 'some 1 other string text with a number' is '1' and this is numeric 1.0&lt;BR /&gt; - part 5 - in string 'yet another test with a 6 number' is '6' and this is numeric 6.0&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Please note that if a part contains a number and a character this will not be seen as numeric...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Kind regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Xander&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 09:14:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180860#M13881</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-11T09:14:32Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting an integer from a string field</title>
      <link>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180861#M13882</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you, Xander.&amp;nbsp; I am totally new to Python and am finding that it is not as intuitive as I had hoped.&amp;nbsp; But, I will see if I can apply what you sent to ArcMap.&amp;nbsp; Maybe I will get ambitious and take one of ESRI's online courses on Python.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Best wishes.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Phil&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 23 Dec 2013 13:50:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180861#M13882</guid>
      <dc:creator>PhilFreeman</dc:creator>
      <dc:date>2013-12-23T13:50:40Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting an integer from a string field</title>
      <link>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180862#M13883</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Thank you, Xander.&amp;nbsp; I am totally new to Python and am finding that it is not as intuitive as I had hoped.&amp;nbsp; But, I will see if I can apply what you sent to ArcMap.&amp;nbsp; Maybe I will get ambitious and take one of ESRI's online courses on Python.&amp;nbsp; &lt;BR /&gt;&lt;BR /&gt;Best wishes.&lt;BR /&gt;&lt;BR /&gt;Phil&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;You could also use regex:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import re
text = 'There is some 234198372 data in here'
print re.findall('\\d+', text)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This would print:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;['234198372']&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The result will be a list of numeric strings. You could convert these to integers.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also &lt;/SPAN&gt;&lt;A href="http://stackoverflow.com/questions/4289331/python-extract-numbers-from-a-string" rel="nofollow noopener noreferrer" target="_blank"&gt;this post&lt;/A&gt;&lt;SPAN&gt; has the following code, which does more or less the same thing without regex:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;gt;&amp;gt;&amp;gt; str = "h3110 23 cat 444.4 rabbit 11 2 dog"
&amp;gt;&amp;gt;&amp;gt; [int(s) for s in str.split() if s.isdigit()]
[23, 11, 2]
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;with the caveat that:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;This will not recognise floats, negative integers, or integers in hexadecimal format. &lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 09:14:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180862#M13883</guid>
      <dc:creator>DouglasSands</dc:creator>
      <dc:date>2021-12-11T09:14:35Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting an integer from a string field</title>
      <link>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180863#M13884</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;1. Right click on your new field, to get to the field calculator&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2. Click the Python radio button&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;3. Check the Show Codeblock box&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;4. In the Pre-Logic Script Code box, copy and paste this function:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def get_num_from_string(string):
&amp;nbsp;&amp;nbsp;&amp;nbsp; '''This function retrieves numbers from a string and converts them to integers'''
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create empty string to store numbers as a string
&amp;nbsp;&amp;nbsp;&amp;nbsp; num = ''
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Loop through characters in the string
&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in string:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # If one of the characters is a number, add it to the empty string
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if i in '1234567890':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; num+=i
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Convert the string of numbers to an integer
&amp;nbsp;&amp;nbsp;&amp;nbsp; integer = int(num)
&amp;nbsp;&amp;nbsp;&amp;nbsp; return integer&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;5. Then in the bottom box, type this (but replace ADDRESS, with the name of your address field):&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;get_num_from_string(!ADDRESS!)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hint: Choose long integer as your datatype for the new field. Or if you want to keep it a string, make the function return "num" instead of "integer"&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 09:14:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180863#M13884</guid>
      <dc:creator>KristenE</dc:creator>
      <dc:date>2021-12-11T09:14:37Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting an integer from a string field</title>
      <link>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180864#M13885</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def get_num_from_string(string):
&amp;nbsp;&amp;nbsp;&amp;nbsp; '''This function retrieves numbers from a string and converts them to integers'''
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create empty string to store numbers as a string
&amp;nbsp;&amp;nbsp;&amp;nbsp; num = ''
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Loop through characters in the string
&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in string:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # If one of the characters is a number, add it to the empty string
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if i in '1234567890':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; num+=i
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Convert the string of numbers to an integer
&amp;nbsp;&amp;nbsp;&amp;nbsp; integer = int(num)
&amp;nbsp;&amp;nbsp;&amp;nbsp; return integer
&lt;/PRE&gt;&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;But this breaks down if the string looks like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;gt;&amp;gt;&amp;gt; text = 'Value&amp;nbsp; = -12.214'
&amp;gt;&amp;gt;&amp;gt; num = get_num_from_string(string)
&amp;gt;&amp;gt;&amp;gt; num
12214
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Which flips the sign and multiples the number by n where n is the number of digits to the right of the decimal.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It will also fail for situations in addresses like '123 72nd St.' which would become 12372.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 09:14:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180864#M13885</guid>
      <dc:creator>DouglasSands</dc:creator>
      <dc:date>2021-12-11T09:14:40Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting an integer from a string field</title>
      <link>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180865#M13886</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;from Phil's 2nd post... where a solution works 95% of the time.. perhaps find a solution for it and then determine whether the remaining 5% has unique characteristics that the first 95% doesn't share... Often a two stage solution is faster and more elegant than a one-pass run.&amp;nbsp; It would be interesting to see this type of dataset broken down on those lines.&amp;nbsp; I am sure that people dealing with geocoding all the time, can come up with examples where failure of the one-step solution occurs all the time.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Jan 2016 01:35:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180865#M13886</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-01-15T01:35:11Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting an integer from a string field</title>
      <link>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180866#M13887</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This is of no help, but I'd like to mention that MapBasic/MapInfo has had the function Val(str), from the start. And it has been of great use to me. Today I miss it in ArcGIS. Incidently, not even FME or Excel seem to have a straightforward transformer or module to do this. Strange indeed.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 17 Feb 2016 12:43:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180866#M13887</guid>
      <dc:creator>MatsElfström</dc:creator>
      <dc:date>2016-02-17T12:43:42Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting an integer from a string field</title>
      <link>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180867#M13888</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;do you mean like int(str) or float(str)... I never liked Val("1") when you wanted a float or Val("1.0") when you wanted an integer.&amp;nbsp; The explicit casting of int(str) and float(str) are nice and for the bold.... complex("1") =&amp;gt; (1+0j) even does casting into the complex plane&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 21 Apr 2017 19:47:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/extracting-an-integer-from-a-string-field/m-p/180867#M13888</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2017-04-21T19:47:52Z</dc:date>
    </item>
  </channel>
</rss>

