<?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: Remove letters and special characters from an alpha numeric string using python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103190#M7946</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am stuck in the same question and trying to remove the alpha characters from the "&lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;House Number&lt;/SPAN&gt;&lt;SPAN&gt;" field. The house number can be like - &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;62/A, A/62, 62A/1&lt;/SPAN&gt;&lt;SPAN&gt; etc. Now, I just want to remove the Alpha Characters only(which can be at any place) and nothing else. Can anyone help me with the code (Python or SQL). &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 03 Feb 2014 03:59:36 GMT</pubDate>
    <dc:creator>shivamparashari</dc:creator>
    <dc:date>2014-02-03T03:59:36Z</dc:date>
    <item>
      <title>Remove letters and special characters from an alpha numeric string using python</title>
      <link>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103183#M7939</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi All, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I need to find a way to remove all letters and special characters from a string so that all i am left with is numbers using python. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The end goal is to use this code in the python code block in the Calculate Field GP tool.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have a field called Lease_Num&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;It contains a string such as ML-26588 , ML 25899, UTLM58778A&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;So sometimes it has a dash, sometimes it has a space and sometime it has nothing.&amp;nbsp; The Alpha characters can be any length and the numbers can be any length.&amp;nbsp; Sometime their can even be an alpha character at the end of the string...its totally random&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I need to strip out the the alpha's and any special characters or spaces from the string to leave me with just the number. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can someone help me with how I would do this and then tell me how to apply it to the code block on Calculate Field GP tool.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Many thanks!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Nov 2012 23:01:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103183#M7939</guid>
      <dc:creator>JessicaKirby</dc:creator>
      <dc:date>2012-11-15T23:01:40Z</dc:date>
    </item>
    <item>
      <title>Re: Remove letters and special characters from an alpha numeric string using python</title>
      <link>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103184#M7940</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Soemthing like this would work:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import string
fieldValue = "ML-26588"
for character in fieldValue:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if character in string.ascii_letters or character in string.punctuation:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldValue = fieldValue.replace(character, "")
print fieldValue&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think it would be better to use something like this in an update cursor though... Somthing like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
#Removes all letters and special characters from a string - hopefully leaving only numbers.
import string, arcpy
updateRows = arcpy.UpdateCursor(myFC)
for updateRow in updateRows:
&amp;nbsp;&amp;nbsp; fieldValue = updateRow.MY_FIELD
&amp;nbsp;&amp;nbsp; for character in fieldValue:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if character in string.ascii_letters or character in string.punctuation:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldValue = fieldValue.replace(character, "")
&amp;nbsp;&amp;nbsp; updateRows.updateRow(updateRow)
del updateRow, updateRows&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:20:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103184#M7940</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2021-12-11T06:20:57Z</dc:date>
    </item>
    <item>
      <title>Re: Remove letters and special characters from an alpha numeric string using python</title>
      <link>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103185#M7941</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;There's always more than one way do something &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's another&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import string
fieldValue = "ML-26588-12-a"
stripChars = fieldValue.translate(None, string.digits)
fieldValue = fieldValue.translate(None, stripChars)
print stripChars
print fieldValue&lt;/PRE&gt;&lt;SPAN&gt;prints:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;ML---a
2658812&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And &lt;/SPAN&gt;&lt;A href="http://stackoverflow.com/questions/1450897/python-removing-characters-except-digits-from-string" rel="nofollow noopener noreferrer" target="_blank"&gt;even more&lt;/A&gt;&lt;SPAN&gt; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:21:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103185#M7941</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2021-12-11T06:21:00Z</dc:date>
    </item>
    <item>
      <title>Re: Remove letters and special characters from an alpha numeric string using python</title>
      <link>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103186#M7942</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;.translate - I like that!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 16 Nov 2012 01:56:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103186#M7942</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2012-11-16T01:56:42Z</dc:date>
    </item>
    <item>
      <title>Re: Remove letters and special characters from an alpha numeric string using python</title>
      <link>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103187#M7943</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;A simple 1 liner will do it: &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;cleanedValue =&amp;nbsp; ''.join([i for i in fieldValue if i.isdigit()])&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;good luck,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 16 Nov 2012 17:48:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103187#M7943</guid>
      <dc:creator>MikeHunter</dc:creator>
      <dc:date>2012-11-16T17:48:51Z</dc:date>
    </item>
    <item>
      <title>Re: Remove letters and special characters from an alpha numeric string using python</title>
      <link>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103188#M7944</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt; ... and then tell me how to apply it to the code block on Calculate Field GP tool.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Expression: &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;ExtractNum(!INFIELD!)&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;Code block:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def ExtractNum(fieldValue):
&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; val = ''.join([i for i in fieldValue if i.isdigit()])
&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; val = None
&amp;nbsp; return val&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:21:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103188#M7944</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T06:21:03Z</dc:date>
    </item>
    <item>
      <title>Re: Remove letters and special characters from an alpha numeric string using python</title>
      <link>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103189#M7945</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;A simple 1 liner will do it: &lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
cleanedValue =&amp;nbsp; ''.join([i for i in fieldValue if i.isdigit()])
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;good luck,&lt;BR /&gt;Mike&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you Mike, this worked like a charm!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:21:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103189#M7945</guid>
      <dc:creator>JessicaKirby</dc:creator>
      <dc:date>2021-12-11T06:21:05Z</dc:date>
    </item>
    <item>
      <title>Re: Remove letters and special characters from an alpha numeric string using python</title>
      <link>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103190#M7946</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am stuck in the same question and trying to remove the alpha characters from the "&lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;House Number&lt;/SPAN&gt;&lt;SPAN&gt;" field. The house number can be like - &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;62/A, A/62, 62A/1&lt;/SPAN&gt;&lt;SPAN&gt; etc. Now, I just want to remove the Alpha Characters only(which can be at any place) and nothing else. Can anyone help me with the code (Python or SQL). &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 03 Feb 2014 03:59:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103190#M7946</guid>
      <dc:creator>shivamparashari</dc:creator>
      <dc:date>2014-02-03T03:59:36Z</dc:date>
    </item>
    <item>
      <title>Re: Remove letters and special characters from an alpha numeric string using python</title>
      <link>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103191#M7947</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Now, I just want to remove the Alpha Characters only(which can be at any place) and nothing else.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;One extremely nice thing about Python is you can easily experiment at an interactive command line and tweak your code to get what you want, for example:&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; ''.join([i for i in "62/A" if i.isdigit()])
'62'
&amp;gt;&amp;gt;&amp;gt; ''.join([i for i in "62/A" if not i.isalpha()])
'62/'&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Can anyone help me with the code (Python or SQL).&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt;SQL is almost always used in ArcGIS to select records - not to calculate fields.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:21:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103191#M7947</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T06:21:08Z</dc:date>
    </item>
    <item>
      <title>Re: Remove letters and special characters from an alpha numeric string using python</title>
      <link>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103192#M7948</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Powerful and flexible function for these kinds of manipulations is the 'sub' function from the 're' (regular expressions) module.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The module is normally installed by default.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import re
re.sub(r'\W', '', "A/62")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;See other uses here:&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://docs.python.org/2/howto/regex.html#matching-characters" rel="nofollow noopener noreferrer" target="_blank"&gt;http://docs.python.org/2/howto/regex.html#matching-characters&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:21:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103192#M7948</guid>
      <dc:creator>FilipKrál</dc:creator>
      <dc:date>2021-12-11T06:21:11Z</dc:date>
    </item>
    <item>
      <title>Re: Remove letters and special characters from an alpha numeric string using python</title>
      <link>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103193#M7949</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks, Mike! It was an agonizing quest before I found your simple answer!&lt;/P&gt;&lt;P&gt;Eric&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 25 Feb 2019 17:53:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103193#M7949</guid>
      <dc:creator>EricMartinson</dc:creator>
      <dc:date>2019-02-25T17:53:30Z</dc:date>
    </item>
    <item>
      <title>Re: Remove letters and special characters from an alpha numeric string using python</title>
      <link>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103194#M7950</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Mike, or anyone, are you still out there? Your solution worked perfectly for me in a field calculation a couple days ago. Now, using the same fields in the same database, but a different selection of records, I'm getting an error. I'm too much of a dolt in Python to see why. Here's what I'm getting:&lt;/P&gt;&lt;P&gt;"syntaxerror: encoding declaration in unicode string (&amp;lt;expression&amp;gt;, line 0)" (see below)&lt;/P&gt;&lt;P&gt;At first I thought it might be some characters that were causing the issue (hyphens, slashes, spaces), but eliminating them didn't help.&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;&lt;P&gt;Eric&lt;/P&gt;&lt;P&gt;&lt;IMG alt="Python field calculation Unicode syntax error" class="image-1 jive-image j-img-original" src="https://community.esri.com/legacyfs/online/438324_Python_field_calc_Unicode_syntax_error.jpg" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 28 Feb 2019 19:11:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/remove-letters-and-special-characters-from-an/m-p/103194#M7950</guid>
      <dc:creator>EricMartinson</dc:creator>
      <dc:date>2019-02-28T19:11:09Z</dc:date>
    </item>
  </channel>
</rss>

