Skip to main content

Library to provide list of Vietnam administrative divisions (tỉnh thành, quận huyện, phường xã).

Project description

image love image pypi common changelog

[Tiếng Việt]

Library to provide list of Vietnam administrative divisions (tỉnh thành, phường xã, after the rearrangement in July 2025) with the name and code as defined by National Statistics Office of Viet Nam.

Example:

{
  "name": "Tuyên Quang",
  "code": 8,
  "codename": "tuyen_quang",
  "division_type": "tỉnh",
  "phone_code": 207,
  "wards": [
    {
      "name": "Xã Thượng Lâm",
      "code": 2269,
      "codename": "xa_thuong_lam",
      "division_type": "xã",
      "short_codename": "thuong_lam"
    },
    {
      "name": "Xã Lâm Bình",
      "code": 2266,
      "codename": "xa_lam_binh",
      "division_type": "xã",
      "short_codename": "lam_binh"
    },
  ]
}

This library provides data in these forms:

  1. JSON

This data is suitable for applications which don’t need to access the data often. They are fine with loading JSON and extract information from it. The JSON files are saved in data folder. You can get the file path via vietnam_provinces.NESTED_DIVISIONS_JSON_PATH variable.

Note that this variable only returns the path of the file, not the content. It is up to application developer to use any method to parse the JSON. For example:

import orjson
import rapidjson
from vietnam_provinces import NESTED_DIVISIONS_JSON_PATH

# With rapidjson
with NESTED_DIVISIONS_JSON_PATH.open() as f:
    rapidjson.load(f)

# With orjson
orjson.loads(NESTED_DIVISIONS_JSON_PATH.read_bytes())
  1. Python data type

This data is useful for some applications which need to access the data more often. There are two kinds of objects, first is the object presenting a single province or ward, second is province code or ward code in form of enum, which you can import in Python code:

>>> from vietnam_provinces import ProvinceCode, Province, WardCode, Ward

>>> Province.from_code(ProvinceCode.P_15)
Province(name='Tỉnh Lào Cai', code=<ProvinceCode.P_15: 15>, division_type=<VietNamDivisionType.TINH: 'tỉnh'>, codename='lao_cai', phone_code=214)

>>> Ward.from_code(23425)
Ward(name='Xã Tu Mơ Rông', code=<WardCode.W_23425: 23425>, division_type=<VietNamDivisionType.XA: 'xã'>, codename='xa_tu_mo_rong', province_code=<ProvinceCode.P_51: 51>)

>>> # Search provinces by name
>>> Province.search('lao cai')
(Province(name='Tỉnh Lào Cai', ...),)

>>> # Search wards by name
>>> Ward.search('phu my')
(Ward(name='Phường Phú Mỹ', ...), Ward(name='Xã Phú Mỹ', ...), ...)

>>> # Search wards by name in a specific province
>>> Ward.search('phu my', province=ProvinceCode(79))  # Hồ Chí Minh City
(Ward(name='Phường Phú Mỹ', ...), ...)

>>> # Search current wards by legacy data (pre-2025)
>>> Ward.search_from_legacy(name='phu my')
(WardWithLegacy(source_code=21730, ward=Ward(name='Xã Phù Mỹ', ...)), ...)

>>> # Get legacy wards that were merged to form a new ward
>>> ward = Ward.from_code(4)  # Phường Ba Đình
>>> ward.get_legacy_sources()
(Ward(name='Phường Trúc Bạch', ...), Ward(name='Phường Quán Thánh', ...), ...)

>>> # Search current wards by legacy district (districts were dissolved in 2025)
>>> Ward.search_from_legacy_district(code=748)  # Thành phố Bà Rịa (old)
(WardWithLegacy(source_code=26710, ward=Ward(name='Phường Bà Rịa', ...)), ...)

>>> # Search current provinces by legacy province code (pre-2025)
>>> Province.search_from_legacy(code=77)  # Tỉnh Bà Rịa - Vũng Tàu
(ProvinceWithLegacy(source_code=77, province=Province(name='Thành phố Hồ Chí Minh', ...)),)

>>> # Get legacy provinces that were merged to form a new province
>>> province = Province.from_code(79)  # Thành phố Hồ Chí Minh
>>> province.get_legacy_sources()
(Province(name='Tỉnh Bình Dương', ...), Province(name='Tỉnh Bà Rịa - Vũng Tàu', ...), Province(name='Thành phố Hồ Chí Minh', ...))

The pre-2025 data types can then be used as:

from vietnam_provinces.legacy import Province, District, Ward
from vietnam_provinces.legacy.codes import ProvinceCode

# Look up by code
province = Province.from_code(ProvinceCode.P_01)

# Iterate over all
for p in Province.iter_all():
    print(p.name)

To know if the data is up-to-date, check the __data_version__ attribute of the module:

>>> import vietnam_provinces
>>> vietnam_provinces.__data_version__
'2026-02-21'

Install

python -m pip install vietnam-provinces
# or
uv add vietnam-provinces

This library is compatible with Python 3.12+.

Development

In development, this project has a tool to scrape and convert data from the National Statistics Office website.

The tool is tested on Linux only (may not run on Windows).

Update data

To scrape data directly from the National Statistics Office website and generate JSON:

python3 -m dev scrape -f nested-json -o vietnam_provinces/data/nested-divisions.json

Or to generate Python code directly:

python3 -m dev scrape -f python

You can run

python3 -m dev scrape --help

to see more options of that tool.

Note that this tool is only available in the source folder (cloned from Git). It is not included in the distributable Python package.

Generate Python code

python3 -m dev scrape -f python

Generate code for pre-2025 data

To generate Python code for pre-2025 administrative divisions (3-level hierarchy: Province -> District -> Ward):

python3 -m dev gen-legacy -c dev/seed-data/Pre-2025-07/Xa_2025-01-04.csv

This generates two files:

  1. vietnam_provinces/legacy/codes.py - Enum definitions for ProvinceCode, DistrictCode, WardCode.

  2. vietnam_provinces/legacy/lookup.py - Lookup mappings for Province, District, Ward objects.

Data source

Credit

Given to you by Nguyễn Hồng Quân, after nights and weekends.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vietnam_provinces-2026.3.0.tar.gz (665.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vietnam_provinces-2026.3.0-py3-none-any.whl (695.7 kB view details)

Uploaded Python 3

File details

Details for the file vietnam_provinces-2026.3.0.tar.gz.

File metadata

  • Download URL: vietnam_provinces-2026.3.0.tar.gz
  • Upload date:
  • Size: 665.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"25.10","id":"questing","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for vietnam_provinces-2026.3.0.tar.gz
Algorithm Hash digest
SHA256 2899e84c47cf6b4d842d523985b25f0ee06b9a87e8569f7a91a9d4007c1bb5c3
MD5 1b695170c02bc23b69dcfe8c09342d79
BLAKE2b-256 11942961ff43a9fa5c6121e0734b1f5e418140fb6468b727cee344e623ed4dd2

See more details on using hashes here.

File details

Details for the file vietnam_provinces-2026.3.0-py3-none-any.whl.

File metadata

  • Download URL: vietnam_provinces-2026.3.0-py3-none-any.whl
  • Upload date:
  • Size: 695.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"25.10","id":"questing","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for vietnam_provinces-2026.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9b10864a2087534318e9c80c573bde090d88ba847d95b47e501012f6650fbc58
MD5 0ddf98b5ff9a5e816e8e017a7cc7c36e
BLAKE2b-256 4cd8c44afaf6945bc39bc38457a5424f42b4d0fe028dc2ba5e9f15b3e4de3814

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page