Deleting last 3 characters using field calculator in ArcGIS Pro (UK Postcode)

2268
5
Jump to solution
07-09-2019 07:12 AM
e_cat
by
New Contributor III

I have no idea how to use Python but I'm pretty sure there would be a way to do this, it's like using the MID query in excel. I'm using ArcGIS Pro and have a field for UK Postcodes, either in the format E4 9SU, DA12 7JS or DA1 7JS. I am trying to use the Field Calculator to delete the last three characters (4 including the space) so I will be left with either E4, DA12 or DA1 

Is anyone able to help in writing the script? as I wouldn't even know where to start. The input field is called Match_Addr so after some research I'm assuming the input would be !Match_addr!

Would appreciate any help with this

0 Kudos
1 Solution

Accepted Solutions
Egge-Jan_Pollé
MVP Regular Contributor

Hi Emily Catt,

As you can see in the screen capture below I did take the content of the field POSTCODE to populate the field POSTCODE_SHORT with only the first part of the postal code.

Using Calculate Field, the very pythonic syntax to accomplish this is:

''.join(!POSTCODE!.split())[:-3]

So in your case that would be:

''.join(!Match_addr!.split())[:-3]

Please refer to this discussion for additional explanation: Python Remove last 3 characters of a string - Stack Overflow 

Hope this helps,

Egge-Jan

View solution in original post

5 Replies
Egge-Jan_Pollé
MVP Regular Contributor

Hi Emily Catt,

As you can see in the screen capture below I did take the content of the field POSTCODE to populate the field POSTCODE_SHORT with only the first part of the postal code.

Using Calculate Field, the very pythonic syntax to accomplish this is:

''.join(!POSTCODE!.split())[:-3]

So in your case that would be:

''.join(!Match_addr!.split())[:-3]

Please refer to this discussion for additional explanation: Python Remove last 3 characters of a string - Stack Overflow 

Hope this helps,

Egge-Jan

e_cat
by
New Contributor III

Thank you for your speedy response Egge-Jan, worked like a charm! Thank you

Egge-Jan_Pollé
MVP Regular Contributor

Hi Emily Catt,

Sounds like your issue has been solved, doesn't it?

Would you please be so kind to mark your question as being answered? This will help the community in finding correct information.

Cheers,

Egge-Jan

Egge-Jan_Pollé
MVP Regular Contributor

Hi Joshua Bixby,

Joshua Bixby markeerde het antwoord van Emily Catt als juist

Why did you mark the "Thank you" as the correct answer, and not the actual answer I gave to the question?

Just curious 🙂

Egge-Jan

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Sorry about that, clicked on the wrong link.  I just updated it.