Calculate Python Right n characters on variable length attribute

162
3
Jump to solution
4 weeks ago
Labels (1)
ThomasHoman
Occasional Contributor

Good Morning All,

This is more of an archiving of knowledge and perhaps a help to others but I would certainly like comments if there is a better or way to do what I needed.

I recently needed to pull the remaining characters out of a variable length text attribute after a certain starting point. The Calculate Field Python examples  was a great starting point with its !SUB_REGION![-3:] for the right most characters but did not fit my situation where the starting attribute contains variable length records.

I needed to grab the remaining characters after character 5. For our Assessor system positions 1-3 = Book, 4-5 = Page, 6-8 or 9 = Parcel depending on if there is an alpha character or not.

APN Sample data:

ThomasHoman_0-1632151901515.png

What I came up with on the Calculate Field is Parcel = !APN![0-len(!APN!)+5:]  This calculates the remaining characters by attribute. By starting with 0 and subtracting the length of the attribute then adding in how many characters to ignore, the remaining characters are copied to the Parcel attribute.

This is what worked for me. In programming there are always alternative methods so please feel free to share. 

Regards,

Tom

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
Regular Contributor

Ummm, what's wrong with !APN![5:] ?


Have a great day!
Johannes

View solution in original post

3 Replies
JohannesLindner
Regular Contributor

Ummm, what's wrong with !APN![5:] ?


Have a great day!
Johannes

View solution in original post

DavidPike
MVP Frequent Contributor

Agree with @JohannesLindner 

0 Kudos
ThomasHoman
Occasional Contributor

Thanks Johannes!

My way was because I am not that familiar with the Python language so though I would post my solution knowing there had to be a better way.

Tom

0 Kudos