I have a series of identification codes that I need to split out. The format of these codes is [region(letter)][district(number)] - [place(number)][subdistrict(letter)]
.
An example of some codes includes S22-201
, TT100-12
, and V6-1B
. Often there is no subdistrict, and all points fall within the same larger district (so no As or Cs or whatever at the end of the string.
I can do parts of it, like splitting at the hyphen.
!Original_ID!.split('-')[0]
and then extracting the district
!Split_ID![1:3]
But it seems like two steps for this are unnecessary, and only works when I know the specific number of characters in the string, which isn't realistic for a large data set.
I'd like to be able to grab each piece at once:
- letters on the left of the hyphen
- numbers on the left of the hyphen
- numbers on the right of the hyphen
- letters (if any) on the right of the hyphen.
I'd need the numeric fields to be integers (or I guess possibly floats in some rare cases maybe).
I am still not doing something correctly. I may need to start smaller and brush up on my Python before I do this, I just assumed this would be a good place to start learning. Here's where I am at, in the Python window in ArcMap.
with arcpy.da.UpdateCursor("Wet_Sub",['Flag_ID','District','Split_ID']) as uCur:
for sRow in uCur:
OrigID = sRow[0].split('-')[0] # first element in the Original_ID
charRng = range(len(OrigID)) # a range to iterate over
Chars = ''
Numbers = ''
for Idx in charRng:
if OrigID[Idx].isnumeric():
Numbers += OrigID[Idx]
else:
chars += OrigID[Idx]
sRow[1] = float(Numbers)
sRow[2] = Chars
uCur.updateRow(sRow)
"Wet_Sub" and 'Flag_ID' are the names of the feature class and actual original field. I also tried to follow along with user2856's suggestion. It looks like I may need to be using both of those code blocks, one pasted into another, but I wasn't sure how to fit them together and what parts to change/remove (e.g. "etc... from code block above").