Skip to main content

Harmonized System (HS) codes from the World Customs Organization with hierarchical structure and search capabilities

Project description

pyhscodes

A Python package for working with Harmonized System (HS) codes from the World Customs Organization. Built on the foundation of the popular pycountry package, pyhscodes provides a similar API for HS codes with hierarchical structure and search capabilities.

Features

  • Complete HS Code Database: Contains 6,940+ HS codes with full hierarchical structure
  • Regulatory Standards: Built-in support for EUDR, EUTR, and CBAM with extensible standards framework
  • Section Support: All 21 HS code sections with descriptions
  • Hierarchical Navigation: Navigate from chapters (2-digit) to headings (4-digit) to subheadings (6-digit)
  • Fuzzy Search: Find codes by description text
  • Fast Lookups: Optimized indexing for quick code retrieval
  • Pythonic API: Clean, intuitive interface similar to pycountry

Installation

pip install pyhscodes

Quick Start

import pyhscodes

# Basic statistics
print(f"Total HS codes: {len(pyhscodes.hscodes)}")
print(f"Total sections: {len(pyhscodes.sections)}")

# Get a specific code
animals = pyhscodes.hscodes.get(hscode="01")
print(f"{animals.hscode}: {animals.description}")
print(f"Section: {animals.section_name}")  # "live animals; animal products"

# Get children of a code
children = pyhscodes.hscodes.get_children("01")
for child in children:
    print(f"  {child.hscode}: {child.description}")

# Get full hierarchy
hierarchy = pyhscodes.hscodes.get_hierarchy("010121")
for code in hierarchy:
    print(f"{code.hscode}: {code.description} (Level {code.level})")

# Fuzzy search
results = pyhscodes.hscodes.search_fuzzy("dairy")
for result in results[:3]:
    print(f"{result.hscode}: {result.description}")

# Check regulatory standards
timber = pyhscodes.hscodes.get(hscode="4401")
print(f"Standards: {timber.standards}")  # e.g., ["EUDR", "EUTR"]
print(f"EUDR applicable: {timber.EUDR}")

API Reference

HS Codes

pyhscodes.hscodes

The main database of HS codes.

Properties:

  • hscode: The HS code (e.g., "010121")
  • description: Description of the code
  • section: Section identifier (e.g., "I", "II")
  • section_info: Full Section object with all section data
  • section_name: Full name of the section
  • section_display_name: Short display name for the section
  • parent: Parent code in the hierarchy
  • level: Code level ("2", "4", or "6")
  • standards: List of applicable regulatory standards (e.g., ["EUDR", "CBAM"])
  • EUDR: Boolean indicating EU Deforestation Regulation applicability
  • EUTR: Boolean indicating EU Timber Regulation applicability
  • CBAM: Boolean indicating Carbon Border Adjustment Mechanism applicability
  • is_chapter: True if this is a 2-digit chapter
  • is_heading: True if this is a 4-digit heading
  • is_subheading: True if this is a 6-digit subheading

Methods:

  • get(hscode="01"): Get a specific code
  • lookup("01"): Lookup by code or description
  • search_fuzzy("horses"): Fuzzy search by description
  • get_by_level("2"): Get all codes at a specific level
  • get_children("01"): Get child codes
  • get_hierarchy("010121"): Get full hierarchy path

Sections

pyhscodes.sections

Database of HS code sections.

Properties:

  • section: Section identifier (e.g., "I", "II")
  • name: Section name/description

Methods:

  • get(section="I"): Get a specific section

Data Structure

HS codes follow a hierarchical structure:

  • Sections: 21 major groupings (I through XXI)
  • Chapters: 97 two-digit codes (01, 02, ..., 97)
  • Headings: 1,229+ four-digit codes (0101, 0102, ...)
  • Subheadings: 5,613+ six-digit codes (010121, 010122, ...)

Example hierarchy:

Section I: Live animals; animal products
├── 01: Animals; live
    ├── 0101: Horses, asses, mules and hinnies; live
        ├── 010121: Horses; live, pure-bred breeding animals
        └── 010129: Horses; live, other than pure-bred breeding animals

Examples

See example.py for a comprehensive demonstration of the package features.

Development

Setting up for development

git clone https://github.com/yourusername/pyhscodes.git
cd pyhscodes
pip install -e .

Running tests

pytest src/pyhscodes/tests/

Data Updates

To update the HS codes database:

  1. Place your CSV files in the project root:

    • harmonized-system-with-standards.csv (columns: section, hscode, description, parent, level, EUDR, EUTR, CBAM)
    • sections.csv (columns: section, name)
  2. Run the conversion script:

    python convert_csv_to_json.py
    

Changelog

2.0.0 (Breaking Change)

  • Added section lookup properties: HS codes now include section_info, section_name, and section_display_name for easy access to section data
  • Added standards array: Each HS code now includes a standards list containing the names of applicable regulatory standards (e.g., ["EUDR", "CBAM"])
  • Added regulatory standard flags: Each HS code now includes boolean fields for EU regulatory standards:
    • EUDR: EU Deforestation Regulation applicability
    • EUTR: EU Timber Regulation applicability
    • CBAM: Carbon Border Adjustment Mechanism applicability
  • Extensible standards: New standards can be added by including uppercase columns in the CSV source
  • Data source changed: Now uses harmonized-system-with-standards.csv instead of harmonized-system.csv
  • Breaking: HS code entries now have additional fields that may affect serialization or data processing

1.0.x

  • Initial release with core HS code database
  • Hierarchical navigation and fuzzy search
  • Section support

License

This project is licensed under the LGPL-2.1 License - see the LICENSE file for details.

Acknowledgments

  • Built on the foundation of pycountry
  • HS codes from the World Customs Organization
  • Inspired by the need for a Python package similar to pycountry but for trade classification codes

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

pyhscodes-2.0.4.tar.gz (186.8 kB view details)

Uploaded Source

Built Distribution

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

pyhscodes-2.0.4-py3-none-any.whl (192.8 kB view details)

Uploaded Python 3

File details

Details for the file pyhscodes-2.0.4.tar.gz.

File metadata

  • Download URL: pyhscodes-2.0.4.tar.gz
  • Upload date:
  • Size: 186.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for pyhscodes-2.0.4.tar.gz
Algorithm Hash digest
SHA256 3e5cab41b6a60e42d50da6991efa3296f9f1b505b17366c9af6e8cad51d156cc
MD5 e4f0cf0d6bfab833f2598ad280240f12
BLAKE2b-256 736b3c7ae6500735a39339ccaeb37cb3966e84c6d8ae561e36102b6b9c75b95a

See more details on using hashes here.

File details

Details for the file pyhscodes-2.0.4-py3-none-any.whl.

File metadata

  • Download URL: pyhscodes-2.0.4-py3-none-any.whl
  • Upload date:
  • Size: 192.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for pyhscodes-2.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6dd6d6c129aff2d6fc6fcbb906341d78d8b65cf2a4b42aff3ada57ab5c0b2803
MD5 f60aa921a3e8131a8aa5353a6518d09f
BLAKE2b-256 76f39d422307b437bbb7cc6bf4964ae06179bf8b90f8b57cab20e5a61aae0050

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