1

Using ArcGIS Desktop 10.2. I have a field in a table of several thousand records. The field is made up of alphanumeric characters. I'd like to perform two queries using python on this field:

  1. Identify the records in this field that contain more than one alpha character

  2. Identify the records in this field that end with more than one alpha character

  • 1
    You could try taking the string, break it up into each individual character stringName*[*index] and run it against a regular expression. You can use that and count the instances and use arcpy.UpdateCursor to assign that value to a new field. You can do the same idea except use negatives to get the end characters. Ie. -1, -2 to count back the last two characters in the string. – Branco Jul 11 '14 at 17:43
  • 1
    How do you want the results outputted? What have you tried? – GISKid Jul 11 '14 at 17:43
  • 1
    Python Regular Expressions:docs.python.org/2/howto/regex.html – Branco Jul 11 '14 at 17:44
  • I want the output to be a selection within the table. Have not tried anything yet but working on it now. – Steve Jul 11 '14 at 17:50
  • 1
    Also forgot.. you would need to set up a arcpy.SearchCursor first to parse through records. Basic process I would take.. Set up a Search Cursor --> Loop through Records and on each iteration --> grab field information --> loop through string characters of that field -- > run each character against a Regex --> update a count field when done --> then take that field string again and check the last two characters against a Regex --> update a flag field for last two alphas --> the script moves to next record. Then select based on attributes after to get your selection. – Branco Jul 11 '14 at 17:51
2

The below function returns the number of alpha characters in a string:

def NumAlhpa(fieldName):
   return len([s for s in fieldName if not s.isdigit()])

you can easily integrate this into the Field calculator:

YourField = NumAlpha( !yourColumnName! )
1

To answer part 1 more directly, you can modify Matej's Field Calculator function to return a boolean indicating whether each record contains more than one alpha character:

def has_multi_alpha(fieldName):
    num_alpha = len([s for s in fieldName if not s.isdigit()])
    if num_alpha > 1:
        return True  # or 1
    else:
        return False  # or 0

To answer part two, you can use the following Field Calculator function, which will check the last two characters (since any others are irrelevant):

def ends_with_two_alpha(fieldName):
    last2 = fieldName[-2:]
    num_alpha = len([s for s in last2 if not s.isdigit()])
    if num_alpha == 2:
        return True  # or 1
    else:
        return False  # or 0
  • nmpeterson, thanks! Between yours and Matej's response, I have exactly what I need :) – Steve Jul 14 '14 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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