Select to view content in your preferred language

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

552
6
04-21-2023 12:08 PM
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 0 ok ( or 1) 22345108A0 ok ( or 0) 6523148000 Not ok ( or 0) 62315010 9 Ok ( or 1) 6218921801 Not ok ( or 0)
6 Replies
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

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

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)``````

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) ``````
Regular Contributor

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

Thanks for the help.

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``````

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!)