9

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.

4 Answers 4

17

I would use regular expressions. If you put the following in the code block section of the calculator it will return a string that has all digits stripped, no matter were they are:

import re
def strip_digits(s):
    return re.sub("\d+", "", s)

This can then be called from the calculate box as:

strip_digits(!column_name!)

If you want to replace characters, add another argument to the function for the replacement characters, and put it in place of the empty string.

0
4

just use strip

s="AAAA-000"
print s.strip("0, 1,2,3,etc")
AAAA-
4

I also found this in the mapping center (arcgis.com).
resources center text management tool

It is old code written for 9.2/9.3. Not sure about appropriateness for 10.x The description reads...

  1. Batch Find and Replace: This tool populates strings in a text field with replacement strings that are specified in a Find/Replace table. This is useful for standardizing label strings or introducing and enforcing standard abbreviations.
  2. Convert Text to Proper Case: This tool converts the text strings in one field (presumed to be all upper or all lower case) to a proper case (mixed upper and lower case) text string in another (new) field. This tool is written to handle common abbreviations such as NE for northeast, so that it won't come out as Ne.
  3. Extract Hwy Numbers for Hwy Shield Labels: Adds the numeric portion of a highway name to another text field.

All these tools are written in Python and can be modified to accomplish any specific needs you have.

3

I would start with this simple case:

>>> x = "A-AA00"
>>> print x
A-AA00
>>> print x.replace("0","")
A-AA
>>> 

From there some of the codeblock examples for Python from the ArcGIS 10 Help should give you a headstart to the total solution.

1
  • 1
    I think the problem with this is that there's no easy way to handle non-0 digits. That's why regular expressions are so useful, as om_henners pointed out.
    – nmpeterson
    Commented Jan 11, 2012 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.