Skip to content

Instantly share code, notes, and snippets.

@gitfvb
Last active February 1, 2024 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitfvb/eb35179beffac8756c9fc899c0387418 to your computer and use it in GitHub Desktop.
Save gitfvb/eb35179beffac8756c9fc899c0387418 to your computer and use it in GitHub Desktop.
Small script to remove linebreaks of the postcode file of https://www.suche-postleitzahl.org

Steps

  1. Execute this line iwr -Uri "https://gist.githubusercontent.com/gitfvb/eb35179beffac8756c9fc899c0387418/raw/50c0a506590e238a5a363438fa83912205b66bd4/reformat.ps1" -UseBasicParsing | iex in PowerShell (tested with 5.1 and Core). A shortened url can be used like iwr -Uri " https://clvr.ch/postcode" -UseBasicParsing | iex
  2. This script downloads and reformats the file to remove linebreaks that could interrupt the interpretation
  3. You will find the final file in your downloads folder with the name plz_einwohner.csv
  4. Upload the file to FastStats and test it with this expression strlist(numericlistfromfile("Public:\plz_einwohner.csv",1,1)) -> replace the , with ; in a German FastStats
  5. With the following expression you could then access the number of residents
# Some settings
$sourceUri = "https://downloads.suche-postleitzahl.org/v2/public/plz_einwohner.csv"
$tempFile = Join-Path -Path $Env:Temp -ChildPath "$( [guid]::NewGuid().toString() ).csv"
$targetFolder = Join-Path -Path $Env:USERPROFILE -ChildPath "Downloads"
$targetFile = Join-Path $targetFolder -ChildPath "plz_einwohner.csv"
# Download the file
Invoke-RestMethod -Method GET -Uri $sourceUri -OutFile $tempFile
# Import the data and reformat it
$columns = [Array]@(
"plz"
@{name="note";expression={ $_.note.replace("`n","|") }}
"einwohner"
@{name="qkm";expression={[int]$_.qkm}}
"lat"
"lon"
)
gc -Path $tempFile -encoding utf8 -raw | ConvertFrom-Csv -Delimiter "," | Select $columns | Export-Csv -Path $targetFile -Encoding UTF8 -Force -Delimiter "`t" -NoTypeInformation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment