Skip to content

Instantly share code, notes, and snippets.

@NickSlash
Created November 24, 2025 00:52
Show Gist options
  • Select an option

  • Save NickSlash/2a07127fa80deed21431642426e3bae3 to your computer and use it in GitHub Desktop.

Select an option

Save NickSlash/2a07127fa80deed21431642426e3bae3 to your computer and use it in GitHub Desktop.
script to transform/filter postcodes.csv - choose columns, filter by postcode district(s) and weather in-use or not.
# https://www.doogal.co.uk/files/postcodes.zip
# 0 Postcode # 21 Lower layer super output area # 42 Plus Code
# 1 In Use? # 22 Rural/urban # 43 Average Income
# 2 Latitude # 23 Region # 44 Sewage Company
# 3 Longitude # 24 Altitude # 45 Travel To Work Area
# 4 Easting # 25 London zone # 46 ITL level 2
# 5 Northing # 26 LSOA Code # 47 ITL level 3
# 6 Grid Ref # 27 Local authority # 48 UPRNs
# 7 County # 28 MSOA Code # 49 Distance to sea
# 8 District # 29 Middle layer super output area # 50 LSOA21 Code
# 9 Ward # 30 Parish Code # 51 Lower layer super output area 2021
# 10 District Code # 31 Census output area # 52 MSOA21 Code
# 11 Ward Code # 32 Index of Multiple Deprivation # 53 Middle layer super output area 2021
# 12 Country # 33 Quality # 54 Census output area 2021
# 13 County Code # 34 User Type # 55 IMD decile
# 14 Introduced # 35 Last updated # 56 Constituency Code 2024
# 15 Terminated # 36 Nearest station # 57 Constituency Name 2024
# 16 Parish # 37 Distance to station # 58 Property Type
# 17 National Park # 38 Postcode area # 59 Roads
# 18 Population # 39 Postcode district # 60 FixPhrase
# 19 Households # 40 Police force # 61 Rural/urban 2021
# 20 Built up area # 41 Water company
Add-Type -AssemblyName Microsoft.VisualBasic
# columns to export (see table above for id)
$columns = @(0, 4, 5)
# postcode districts to export
$postcodes = @("CB1","CB2","CB23","CB24","CB3","CB4","PE27","PE28","PE29")
# filter out-of-use postcodes
$filter = $true
$source = Get-Item "$PSScriptRoot\postcodes.csv"
$output = "$PSScriptRoot\output.csv"
$parser = [Microsoft.VisualBasic.FileIO.TextFieldParser]::new($source)
$writer = [System.IO.StreamWriter]::new($output)
try {
$parser.TextFieldType = [Microsoft.VisualBasic.FileIO.FieldType]::Delimited
$parser.SetDelimiters(",")
$parser.HasFieldsEnclosedInQuotes = $true
$headers = $parser.ReadFields()
$selected = $columns | ForEach-Object { $headers[$_] }
$writer.WriteLine($selected -join ",")
while (!$parser.EndOfData) {
$fields = $parser.ReadFields()
if ($filter -and ($fields[1] -eq "No")) { continue }
if (($postcodes -contains $fields[39])) {
$selected = $columns | ForEach-Object { $fields[$_] }
$writer.WriteLine($selected -join ",")
}
}
}
finally {
$parser.Dispose()
$writer.Dispose()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment