Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
add comment

4 Answers

up vote 12 down vote accepted

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.

share|improve this answer
4  
absolutely!, you want regex to handle these character parsing issues. –  Brad Nesom Jan 11 '12 at 14:23
1  
I added a link to the regular expressions how-to in the Python docs for the uninitiated. –  blah238 Jan 11 '12 at 18:39
    
Thanks for that @blah238 –  om_henners Jan 11 '12 at 21:59
add comment

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.

share|improve this answer
add comment

just use strip

s="AAAA-000"
print s.strip("0, 1,2,3,etc")
AAAA-
share|improve this answer
add comment

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.

share|improve this answer
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 Jan 11 '12 at 16:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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