Splitting addresses with str.split(",") breaks as soon as a row has no commas, a flat number, or a German street. This script sends each row to the parseaddr API and adds the fields as new columns. It needs pandas and requests.
order_id,address
1001,"flat 2, 10 downing st london sw1a 2aa"
1002,"221B BAKER STREET LONDON NW16XE"
1003,"Hauptstraße 5, 10115 Berlin, Germany"
import os
import pandas as pd
import requests
session = requests.Session()
session.headers["Authorization"] = f"Bearer {os.environ['PARSEADDR_API_KEY']}"
FIELDS = ["unit", "house_number", "road", "city", "postcode", "country"]
def parse(address):
r = session.post("https://api.parseaddr.com/v1/parse", json={"address": address}, timeout=10)
r.raise_for_status()
parsed = r.json()["parsed"]
return pd.Series({f: parsed.get(f) for f in FIELDS})
df = pd.read_csv("orders.csv")
df = df.join(df["address"].apply(parse))
df.to_csv("orders_split.csv", index=False)
print(df[["order_id"] + FIELDS].to_string(index=False))
order_id unit house_number road city postcode country
1001 flat 2 10 downing st london sw1a 2aa NaN
1002 NaN 221b baker street london nw16xe NaN
1003 NaN 5 hauptstraße berlin 10115 germany
Run it with PARSEADDR_API_KEY=pa_... python split.py. It writes orders_split.csv next to the input.
.str.upper() on the postcode column if you need it.country is empty when the address does not name one. If you know the country, send "country": "gb" with the address and read country_code from the response.Retry-After header; raise_for_status() stops the script there before anything is written, so wait and run it again.The Parserator add-on for Sheets is built on usaddress, which is written for US addresses. On the 200 UK addresses in the comparison, usaddress got 0 fully right (results). This Apps Script adds a =PARSEADDR() formula that calls parseaddr instead.
PARSEADDR_API_KEY with your key. The key stays out of the cells.=PARSEADDR(A2:A100). Six columns fill to the right: unit, house number, road, city, postcode, country.// Google Sheets: Extensions > Apps Script, paste this, save.
// Project Settings > Script properties: add PARSEADDR_API_KEY = your key.
// In a cell: =PARSEADDR(A2:A100) -> unit, house_number, road, city, postcode, country
const FIELDS = ["unit", "house_number", "road", "city", "postcode", "country"];
function PARSEADDR(input) {
const key = PropertiesService.getScriptProperties().getProperty("PARSEADDR_API_KEY");
const rows = Array.isArray(input) ? input.map(r => String(r[0]).trim()) : [String(input).trim()];
const cache = CacheService.getScriptCache();
const out = rows.map(a => (a ? JSON.parse(cache.get(a.slice(0, 200)) || "null") : FIELDS.map(() => "")));
const todo = rows.map((a, i) => i).filter(i => out[i] === null);
for (let s = 0; s < todo.length; s += 10) { // free key: 10 calls a second
if (s > 0) Utilities.sleep(1000);
const batch = todo.slice(s, s + 10);
const res = UrlFetchApp.fetchAll(batch.map(i => ({
url: "https://api.parseaddr.com/v1/parse", method: "post", contentType: "application/json",
headers: { Authorization: "Bearer " + key }, payload: JSON.stringify({ address: rows[i] }),
muteHttpExceptions: true,
})));
res.forEach((r, j) => {
const i = batch[j];
if (r.getResponseCode() !== 200) { out[i] = ["error " + r.getResponseCode(), "", "", "", "", ""]; return; }
const p = JSON.parse(r.getContentText()).parsed;
out[i] = FIELDS.map(f => p[f] || "");
cache.put(rows[i].slice(0, 200), JSON.stringify(out[i]), 21600); // 6 hours: recalcs cost nothing
});
}
return out;
}
| A: address | unit | house_number | road | city | postcode | country |
|---|---|---|---|---|---|---|
| flat 2, 10 downing st london sw1a 2aa | flat 2 | 10 | downing st | london | sw1a 2aa | |
| 221B BAKER STREET LONDON NW16XE | 221b | baker street | london | nw16xe | ||
| Hauptstraße 5, 10115 Berlin, Germany | 5 | hauptstraße | berlin | 10115 | germany |
CacheService, so a recalculation does not spend calls again.parseaddr · home · agents · pricing · llms.txt · .md
Pyke Limited, registered in England and Wales, company number 09748427.