Data Package

1.15.4 · active · verified Fri Apr 17

The `datapackage-py` library is a Python implementation of the Data Package standard, focusing on utilities to create, read, and validate data packages as defined by frictionlessdata.io specifications. It provides a simple API for interacting with `datapackage.json` files and their associated resources. The current version is 1.15.4, and it follows a minor release cadence as needed for bug fixes and small enhancements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a `datapackage.json` and a resource file, then load, validate, and read data from a `datapackage.Package` object. It includes an example of accessing resources and iterating through their data.

from datapackage import Package, Resource
import json
import os

# Create a simple data package descriptor in memory
descriptor = {
    'name': 'my-data-package',
    'resources': [
        {
            'name': 'cities',
            'path': 'cities.csv',
            'profile': 'tabular-data-resource',
            'schema': {
                'fields': [
                    {'name': 'id', 'type': 'integer'},
                    {'name': 'name', 'type': 'string'}
                ]
            }
        }
    ]
}

# Simulate a file on disk (or create it for real)
csv_data = "id,name\n1,London\n2,Paris"
with open('cities.csv', 'w') as f:
    f.write(csv_data)

with open('datapackage.json', 'w') as f:
    json.dump(descriptor, f, indent=2)

# Load the data package
package = Package('datapackage.json')

# Validate the package
if package.valid:
    print(f"Package '{package.name}' is valid!")
else:
    print("Package validation errors:")
    for error in package.errors:
        print(f"- {error}")

# Get a resource
cities_resource = package.get_resource('cities')

# Read data from the resource
print("\nCities data (keyed=True):")
for row in cities_resource.read(keyed=True):
    print(row)

# Clean up created files
os.remove('cities.csv')
os.remove('datapackage.json')

view raw JSON →