UK address format, and the fields a parser returns for it

parseaddr.com · plain markdown

A UK address runs from the most specific line to the most general: the recipient, a company if there is one, the house number and street, a district, the town, an optional county, then the postcode on its own line. A house name goes on the line above the street. Written on one line, each line break becomes a comma (Wikipedia, citing Royal Mail's guidance).

Wikipedia's example, line by line, with the field /v1/parse returns for each line when the address after the name is sent as one string:

LineExampleField returned
RecipientMr A. Paynenot sent
Company or house nameARAMARK Ltd.house: aramark ltd.
Number and street30 Commercial Roadhouse_number: 30, road: commercial road
DistrictFrattonsuburb: fratton
Post townPORTSMOUTHcity: portsmouth
CountyHampshirestate_district: hampshire
PostcodePO1 1AApostcode: po1 1aa
CountryUNITED KINGDOMcountry: united kingdom

Get a free key Parse your own address

What /v1/parse returns for ten UK formats

Values come back in lower case and keep the input's spelling.

FormatInputFields returned
Number and street10 Downing Street, London SW1A 2AA{"house_number": "10", "road": "downing street", "city": "london", "postcode": "sw1a 2aa"}
FlatFlat 2, 14 High Street, Oxford OX1 4AG{"unit": "flat 2", "house_number": "14", "road": "high street", "city": "oxford", "postcode": "ox1 4ag"}
Lower case, no commas221b baker street london nw1 6xe{"house_number": "221b", "road": "baker street", "city": "london", "postcode": "nw1 6xe"}
Upper case25 DEANSGATE, MANCHESTER, M3 4LQ{"house_number": "25", "road": "deansgate", "city": "manchester", "postcode": "m3 4lq"}
Postcode with no space48 Princes Street Edinburgh EH22YJ{"house_number": "48", "road": "princes street", "city": "edinburgh", "postcode": "eh22yj"}
House name, no numberRose Cottage, Church Lane, Bampton, OX18 2LZ{"house": "rose cottage", "road": "church lane", "city": "bampton", "postcode": "ox18 2lz"}
PO boxPO Box 61, Leeds LS1 6HD{"po_box": "po box 61", "city": "leeds", "postcode": "ls1 6hd"}
Country on the end19 Donegall Place, Belfast, BT1 5AB, United Kingdom{"house_number": "19", "road": "donegall place", "city": "belfast", "postcode": "bt1 5ab", "country": "united kingdom"}
Croydon (London-area post town)67 Park Lane, Croydon CR0 1JD{"house_number": "67", "road": "park lane", "city_district": "croydon", "postcode": "cr0 1jd"}
Business unit on an estateUnit 4, Riverside Industrial Estate, Station Road, Reading RG1 1LG{"house_number": "unit 4", "road": "riverside industrial estate station road", "city": "reading", "postcode": "rg1 1lg"}

Three cases to handle in your code

A postcode with no space. EH22YJ comes back as eh22yj. Every UK postcode ends in a digit and two letters, so put the space before the last three characters:

import re

def uk_postcode(p):
    p = re.sub(r"\s+", "", p).upper()
    return p[:-3] + " " + p[-3:]

uk_postcode("eh22yj")  # "EH2 2YJ"

Croydon and other London-area towns. 67 Park Lane, Croydon CR0 1JD returns croydon as city_district, not city. Croydon is its own post town: 60% of Greater London's area has postcodes outside the London post town, in 13 other areas such as CR, BR and KT (Wikipedia). Read city and fall back to city_district:

town = parsed.get("city") or parsed.get("city_district")

A unit on an estate. Unit 4, Riverside Industrial Estate, Station Road returns unit 4 as house_number and joins the estate and the street into one road value. If you store the estate name on its own, split it out of road yourself.

UK postcode formats

A postcode is an outward code of two to four characters, a space, and an inward code of exactly three characters. There are six formats, where A is a letter and 9 is a digit (Wikipedia):

FormatExample
A9 9AAM1 1AE
A99 9AAB33 8TH
A9A 9AAW1A 0AX
AA9 9AACR2 6XH
AA99 9AADN55 1PT
AA9A 9AAEC1A 1BB

Question

How do I write a UK address on one line?

Keep the same order and put a comma where each line break was, for example 30 Commercial Road, Fratton, Portsmouth, PO1 1AA.

Call it

curl -s https://api.parseaddr.com/v1/parse \
  -H "Authorization: Bearer $PARSEADDR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"address": "Flat 2, 14 High Street, Oxford OX1 4AG"}'

The free tier is 2,000 calls a month. See pricing for paid plans.