Select to view content in your preferred language

Find records with number/character that should not be there.

627
6
04-21-2023 12:08 PM
CCWeedcontrol
Regular Contributor

I have a filed that I need to find records in a layer that have a number that should not be there. Not really sure on how to do this.

Rec field has length of 10. In the example below the first and fourth records has 0 in the 9th space (it could be a different digit) and some records will have an alph character in the 9th space, which is ok as long as it's an alph character. I would like to somehow mark the records.  Essentially, I need to find which records have a digit(skip if alph character is there) on the 9th space of the records. Hopefully this makes sense.

Rec

mark

12345678 0ok ( or 1)
22345108A0ok ( or 0)
6523148000Not ok ( or 0)
62315010 9Ok ( or 1)
6218921801Not ok ( or 0)
0 Kudos
6 Replies
DavidPike
MVP Frequent Contributor

Using Field Calculator in the attribute table https://pro.arcgis.com/en/pro-app/latest/help/data/tables/fundamentals-of-field-calculations.htm

or GP tool https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/calculate-field.htm

Calculate into the new field you've created for this i.e. 'mark'.

Expression type = Python 3

enter this in the box below "=" 

digitron(!tableName.rec or whatever your field is called - double click on the fields window to auto add the field!)

enter this in the 'Code Block' window:

def digitron(rec):
  try:
    if rec[8].isnumeric():
      return 'Not OK'
    else:
      return 'OK'
  except:
    return 'Some error'

validate then apply

 

 

CCWeedcontrol
Regular Contributor

This worked in Arcmap, but how can run it outside.

I tired the following but I get "Parameters are not valid." or with updatecursor?

 

import arcpy

inTable = "fc"
fieldName = "rec"
expression = "digitron(rec)"

codeblock = """
def digitron(rec):
  try:
    if rec[8].isnumeric():
      return 'Not OK'
    else:
      return 'OK'
  except:
    return 'Some error' """

arcpy.management.CalculateField(inTable, fieldName, expression, "PYTHON3", codeblock)

 

0 Kudos
DavidPike
MVP Frequent Contributor
import arcpy

in_table = "fc"
field_names = ["rec", "mark?"]

with arcpy.da.UpdateCursor(in_table, field_names) as cursor:
    for row in cursor:
        try:
            if (row[0])[8].isnumeric():
                row[1] = 'Not OK'
            else :
                row[1] = 'OK'
        except:
            row[1] = 'Some error'
        cursor.updateRow(row) 
0 Kudos
CCWeedcontrol
Regular Contributor

oh I see, line 9  (row[0])[8].isnumeric.

Thanks for the help.

0 Kudos
Jordan_Silver
New Contributor

Hello!  I think this function will do what your looking for:

def mark_records(records):
    result = []
    for rec in records:
        # Check if the 9th character (index 😎 is an alphabetic character
        if rec[8].isalpha() or rec[8].isspace():
            # Mark as ok
            result.append({"Rec": rec, "Mark": "ok"})
        elif rec[8].isdigit():
            # Mark as Not ok
            result.append({"Rec": rec, "Mark": "Not ok"})
        else:
            # If neither alphabetic nor digit nor space, mark as Invalid
            result.append({"Rec": rec, "Mark": "Invalid"})
    return result

 

0 Kudos
CCWeedcontrol
Regular Contributor

I tried you function but I couldn't get it to work right.

pre-logic script code:

 

def mark_records(records):
    result = []
    for rec in records:
        # Check if the 9th character (index  is an alphabetic character
        if rec[8].isalpha() or rec[8].isspace():
            # Mark as ok
            result.append({"Rec": rec, "Mark": "ok"})
        elif rec[8].isdigit():
            # Mark as Not ok
            result.append({"Rec": rec, "Mark": "Not ok"})
        else:
            # If neither alphabetic nor digit nor space, mark as Invalid
            result.append({"Rec": rec, "Mark": "Invalid"})
    return result

Field = (!rec!)

 

0 Kudos