Skip to main content

Swiss zipcodes database

This library provides an easy way to look up a swiss zipcode and get its canton or municipality. Use it like this:

>>> from zipch import ZipcodesDatabase
>>> zd = ZipcodesDatabase('/tmp/zipcodes')
>>> zd.get_location(1003)
Location(official_name='Lausanne', canton='VD', municipality='Lausanne', coordinates=Lv95Coordinates(E=Decimal('2537956.3654948957'), N=Decimal('1152398.7080000006')))

Installation

Zipch has been tested on Python 3.7+. The easiest way to install it is by using PyPI:

pip install zipch

Usage

Start by creating a ZipcodesDatabase object. In the example below, /tmp/zipcodes is a file that will be used as the zipcodes database. If the file doesn't exist yet, it will be created by downloading the latest version of the zipcodes database.

>>> from zipch import ZipcodesDatabase
>>> zd = ZipcodesDatabase('/tmp/zipcodes')

You can then get all the zipcodes registered in the database as a {zipcode: location} dict:

>>> zd.get_locations()
{8192: Location(official_name='Zweidlen', canton='ZH', municipality='Glattfelden'), 8193: Location(official_name='Eglisa', canton='ZH', municipality='Eglisa'), ...}

The library packs with some utility functions, these are all things that can be derived from get_locations() but that are here for convenience:

>>> zd.get_location(1003)
Location(official_name='Lausanne', canton='VD', municipality='Lausanne', coordinates=Lv95Coordinates(E=Decimal('2537956.3654948957'), N=Decimal('1152398.7080000006')))
>>> zd.get_zipcodes_for_municipality('Lausanne')
[1000, 1003, 1004, 1005, 1007, 1010, 1011, 1018, 1012]
>>> zd.get_zipcodes_for_canton('VD')
[1412, 1428, 1430, 1441, 1450, 1114, 1000, 1003, ...]
>>> zd.get_cantons()
['AG', 'AI', 'AR', 'BE', 'BL', 'BS', ...]
>>> zd.get_municipalities()
['Aadorf', 'Aara', 'Aarberg', 'Aarburg', 'Aarwangen', ...]

Geolocation

Location objects also have a coordinates attribute, which contains the coordinates in LV95 format. You can use the lv95_to_wgs84 function to convert these coordinates to regular WGS84 longitude & latitude coordinates:

>>> from zipch import ZipcodesDatabase, lv95_to_wgs84
>>> lv95_to_wgs84(ZipcodesDatabase("/tmp/zipcodes").get_location(1003).coordinates)

Coordinates in regular WGS84 format are available in the wgs84_coordinates attribute.

Release files for zipch 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for zipch 2.0.1
File Size Uploaded
zipch-2.0.1.tar.gz 5.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for zipch 2.0.1
File Interpreter ABI Platform
zipch-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.0 kB

Release files / zipch-2.0.1.tar.gz

Download URL zipch-2.0.1.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
4fe89b79727f533a7687aa840a8717ac466fda7c6b18b7aeeeda55069229f43d
BLAKE2b-256 checksum
How to use checksums
5c43e8845f355d3ae762e0cc50893d7d9dae6ac568684d85376c53418c8b2606
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.9

Release files / zipch-2.0.1-py3-none-any.whl

Download URL zipch-2.0.1-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
650218e905ebe7ce1633f90e9dd95db2bcabe787b01bec4ff37d3a4e13881f9b
BLAKE2b-256 checksum
How to use checksums
abd1ec514e5d7b726b773fdfe6e188afd5d024a30f47691d23c1e6a7a8f2b9c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.9

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 release files

2.0.0

2 release files

1.1.0

2 release files

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page