3

I have a number of UK postcodes. However, they are not well formatted. Sometimes, they look normal postcodes such as SW5 2RT, sometimes they don't have the space in the middle, but still valid such as SW52RT. But in some cases, they are wrong due to human factor, and they look like ILovePython which is totally invalid as postcodes.

So, I am wondering how can I validate the postcodes efficiently (using Python)?

Many thanks.

=================================================================

EDIT:

Thanks for the answers at: this page. But it seems only check characters in the postcode whether they are alphabet or numbers, but don't care if the combination make sense. There could be a false postcode such as AA1 1AA that get passed through the validation.

Community
  • 1
  • 1
ChangeMyName
  • 7,018
  • 14
  • 56
  • 93
  • 4
    Just use regular expression and check out the question here: http://stackoverflow.com/questions/164979/uk-postcode-regex-comprehensive – cansik Sep 01 '15 at 08:42
  • There's some package and code snippets out there in order to validate UK postcode. Why don't you use them? – Lancelot HARDEL Sep 01 '15 at 08:42
  • The simplest way to validate addresses is by sending a letter and getting the recipient to verify that they've received it. – Peter Wood Sep 01 '15 at 09:10
  • If you send a letter (in the UK) with an invalid post code it will probably reach the recipient, but likely it will be delayed in the post because the faulty post code causes it to be routed incorrectly or manually sorted. – nigel222 Sep 01 '15 at 10:51

3 Answers3

6

You can use this package for your purpose:

uk-postcode-utils 0.1.0

Here it is links for the same:

https://pypi.python.org/pypi/uk-postcode-utils

Also please have a look at:

Python, Regular Expression Postcode search

Community
  • 1
  • 1
Murali
  • 1,084
  • 1
  • 11
  • 28
1

You are correct to say that Regex validation goes only so far. To ensure that the post code is 'postally' valid, you'll need a reference set to validate it against. There are a large number (low thousands) of changes to UK addresses per day to keep track of, and I do not believe this is something a regex alone can solve.

There are a couple of ways you can do it, either use a 3rd party to help you capture a complete & correct address (many available including https://www.edq.com/uk/products/address-validation/real-time-capture (my company)), or get a data supply from Royal Mail and implement your own solution.

Coping with typo's and different formats shouldn't be a problem either way you do it. Most 3rd parties will do this easily for you and should be able to cope with some mistakes too (depending upon what you have to search upon). They'll all have web services you should be able to easily implement or grab integration snippets for.

Al Mills
  • 1,072
  • 6
  • 22
1

The UK Office for National Statistics publishes a list of UK postcodes, both current and retired, so you could pull the relevant columns out of the latest .csv download, duplicate the current ones with the space(s) removed and then do a lookup (it might be best to use a proper database such as MySQL with an index for this).

xnx
  • 24,509
  • 11
  • 70
  • 109
  • This solution is broken for postcodes introduced after the published list of postcodes which can be years out of date – 0x777C Aug 02 '21 at 13:18