removing numeric characters from alphanumeric string in field calculator ArcMap 10

3840
7
01-10-2012 09:10 PM
wilfredwaters
New Contributor III
I have a field containing alphanumeric values in the following format:

A-AA00

Sometimes they are also

A-AAA0

I need a python script I can put in the field calculator in ArcMap 10 that will at least remove the numeric characters, and retrain the alphabetical ones and the hyphen. So that, for example, this:

A-AA0

becomes this:

A-AA

I also want to replace the numeric chars with numeric chars from another field. This Stack Exchange entry addresses this issue, but the string has a space before the number and this can be used for splitting and indexing... my strings are not so handily constructed.
Tags (2)
0 Kudos
7 Replies
JasonScheirer
Occasional Contributor III
Try something like this in the field calculator.


Expression:

ReplaceLastNumber(!textfield!, !numberfield!)

Code block:

import re
def ReplaceLastNumber(text_value, number_value):
      numbers = re.findall("\d+", text_value)
      if numbers:
          last_number = numbers[-1]
          last_number_index = text_value.rfind(last_number)
          return text_value[:last_number_index] + str(number_value) + text_value[last_number_index + len(last_number):]
      else:
          return text_value
0 Kudos
curtvprice
MVP Esteemed Contributor
Try something like this in the field calculator.


Expression:

ReplaceLastNumber(!textfield!, !numberfield!)

Code block:

import re
def ReplaceLastNumber(text_value, number_value):
      numbers = re.findall("\d+", text_value)
      if numbers:
          last_number = numbers[-1]
          last_number_index = text_value.rfind(last_number)
          return text_value[:last_number_index] + str(number_value) + text_value[last_number_index + len(last_number):]
      else:
          return text_value


Thanks Jason! The re module is very useful but rough going when  you don't use it often, so the code snippet is very much appreciated. (posting with a new subject line so others can find it!
0 Kudos
EricaBickford
New Contributor
I have a somewhat related question, though it pertains to Arcmap 9.3.1.

I have a field [Col_Row] that gives the column and row of a shapefile grid in the format of 23-999.

I want to extract the columns and rows into their own fields such that "23-999" becomes "23" and "999" using "-" as a delimiter. The problem is that the lengths are variable, eg the field can contain values like "1-2", "351-7", "81-253" which makes substring, trim, left and right commands tricky.

I've tried using substring as follows:
substring([Col_Row],1,instr([Col_Row],'-')-1)

But I get a VBA error...

Any help would be much appreciated. Such a simple task, and yet I've already spent hours trying to do it.
0 Kudos
curtvprice
MVP Esteemed Contributor
I want to extract the columns and rows into their own fields such that "23-999" becomes "23" and "999" using "-" as a delimiter. The problem is that the lengths are variable, eg the field can contain values like "1-2", "351-7", "81-253" which makes substring, trim, left and right commands tricky.


I suggest doing the Calculate Field using Python syntax instead of VBScript (the default for Calculate Field). The split function makes this easier:

!Col_Row!.split("-")[0]
!Col_Row!.split("-")[1]
0 Kudos
EricaBickford
New Contributor
Thanks curtvprice,

But I didn't think it was possible to use python with Arc 9.3.1....is it?
0 Kudos
curtvprice
MVP Esteemed Contributor
But I didn't think it was possible to use python with Arc 9.3.1....is it?


Sure can, see the Arc 9.3 doc for Calculate Field

The VBScript syntax you're looking for is this. Note the Mid() function and the double-quote.
Mid([Row_Col],1,InStr([Row_Col],"-")-1)


Here's my bookmarked VBScript reference:
http://www.w3schools.com/vbscript/vbscript_ref_functions.asp
0 Kudos
EricaBickford
New Contributor
Ah, mid! I've done this before, I just couldn't remember which function I'd used...

I looked into the python-field calculator documentation you linked - as using python would really be much better - but my field calculator wouldn't accept the python code. I'll have to look into whether some kind of python update or plugin is required. It looks like Arc 10 allows you to indicate that you are using python...

In the meantime, thanks very much for the VB help!
0 Kudos