Extracting certain string/characters and numbers from field

1988
14
Jump to solution
09-27-2019 03:14 PM
2Quiker
Occasional Contributor II

I have a field with with descriptions and i need to extract the tax numbers from this field and put them into another field in the same layer. 

Some examples of what i need .

1. 

12-5N-5W SW NW SW S & W OF CANAL,, TX 2 IN SWNW LS TX 2-A

- I need to extract "TX 2" from this field

2. 

23-3N-2W SW 4TH ST TOWNHOMES TX 11271 IN LT 4C BLK 1

- I need to extract "TX 11271" from this row

3. 

04-5N-5W SE TAX 3 & TAX 4 IN NWSE

- I need to extract "TAX 3, TAX 4" from this row

4. 

21-4N-3W SW TX 86, 89, 90 & 93 IN S 1/2 OF SW

- I need to exact "TX 86, 89,90 & 93" from this row

I currently can extract numbers with the following and i need help to include what i mentioned above.  I guess i need to extract everything beginning at TX and before the space before the next alpha characters. 

fc = r'C:\Temp\Parcels.shp'

with arcpy.da.UpdateCursor(fc,['Legal','Legal2']) as cursor:
    for row in cursor:
        row[1] = ''.join([str(i) for i in row[0] if i.isdigit()])
        cursor.updateRow(row)
0 Kudos
14 Replies
2Quiker
Occasional Contributor II

Joshua i think you are right there might be some case that i'll have to manually address.

question if you don't mind, based on your code you mentioned that i wouldn't be able to  pass a list of values directly into a single field with the update cursor do you mind sharing how i can do this please?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Take the following result, for example:

['30-A-1-D', '30-A-1-D-A', '30-A-1-F']

Given a list of strings, you can concatenate/join the strings together using a separator, and then put that single string into a single text field.

>>> ", ".join(['30-A-1-D', '30-A-1-D-A', '30-A-1-F'])
'30-A-1-D, 30-A-1-D-A, 30-A-1-F'
>>>

0 Kudos
2Quiker
Occasional Contributor II

OK, i wasn't sure what you meant by that.

One more question if you don't mind. How can i add 'TAX' to the out puts based on your code?

With the following i get

'30-A-1-D', '30-A-1-D-A', '30-A-1-F'

with arcpy.da.UpdateCursor(recs,['Legal','Legal2']) as cursor:
        for row in cursor:
            row[1] = ','.join(extract_tax_ids(pattern.split(row[0].upper()))).strip()
            cursor.updateRow(row)

I want to get TAX 30-A-1-D, TAX-A-1-D-A, TAX-30-A-1-F, etc.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

There are a few different ways.  Sticking with str.join(), you could use:

"TAX " + ", TAX ".join(['30-A-1-D', '30-A-1-D-A', '30-A-1-F'])

0 Kudos
2Quiker
Occasional Contributor II

I wasn't playing the comma correctly in "TAX " + ", TAX ". I had it "TAX " +, " TAX ", thanks.

Although it put "TAX" in every row even one's that didn't have any tax numbers i just another udpatecursor to remove all the extra values with just "TAX".

0 Kudos