1

I have a field that has values such as DIMP 1234567 N. Eucla Ave/ W. Bonita Ave or DIMP Seg #1234567- Spinnaker Dr. Ventur or DIMP SEG 1234567 PYRITES AND ESMERALDA. I want to extract only the numerical values and update another field with those numerical values of the same row. The field that has the numerical values is called WORK_ORDER_REF and the field that I want to update with the numerical value is DIMP_SEGMENT. A variation of the code below updated the DIMP_SEGMENT field only with the first value of my list.

import arcpy

a = []
table = "table"
with arcpy.da.UpdateCursor(table, ["WORK_ORDER_REF_DESCRIPTION", "DIMP_SEGMENT"]) as cursor:
     dimpsegs= [row[0] for row in cursor]
     for seg in dimpsegs:
        line = seg
        for word in line.split():
           try:
              a.append(int(word))
           except ValueError:
             pass
        for row in cursor:
            row[1] = a
            cursor.updateRow(row) 

2 Answers 2

3

Make use of regular expressions to find your number. .split () won't work for numbers like #1234567 because of the pound symbol. Also you only need to iterate your cursor once.

import re
import arcpy

with arcpy.da.UpdateCursor(table, ["WORK_ORDER_REF_DESCRIPTION", "DIMP_SEGMENT"]) as cursor:
    for desc, seg in cursor:
        seg = re.search (r"\d+", desc).group ()
        row = (desc, seg)
        cursor.updateRow (row)
0

When you do dimpsegs= [row[0] for row in cursor] you are reaching the last row of your cursor, meaning that you need to create it again or use its reset() method to resets the cursor back to the first row and update all the values in DIMP_SEGMENT field.

Try something like this:

import arcpy

table = "table"
with arcpy.da.UpdateCursor(table, ["WORK_ORDER_REF_DESCRIPTION", "DIMP_SEGMENT"]) as cursor:
     dimpsegs= [row[0] for row in cursor]
     for row in cursor:
        a = [] # moved here as we need to clean it for each row!
        line = row[0]
        for word in line.split():
           try:
              a.append(int(word))
              row[1] = a
              cursor.updateRow(row) 
           except ValueError:
             pass

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.