The Changelog

Open Source moves fast. Keep up.

Tablib: Pythonic Tabular Data Library

The Changelog’s Kenneth Reitz is pleased to announce a new Python module: Tablib. Tablib is a simple module for working with tabular datasets. It allows you create tables of data using standard Python datatypes, manipulate them, and easily export to Excel, JSON, YAML, and CSV.

Basic Usage:

import tablib

headers = ('first_name', 'last_name', 'gpa')
data = [('John', 'Adams', 90), ('George', 'Washington', 67)]

data = tablib.Dataset(*data, headers=headers)

You can maniuplate your data like a standard Python list:

>>> data.append(('Henry', 'Ford', 83))

>>> print data['first_name']
['John', 'George', 'Henry']

>>> del data[1]

You can easily export your data to JSON, YAML, XLS, and CSV.

>>> print data.json
[{"first_name": "John", "last_name": "Adams", "gpa": 90}, {"first_name": "Henry", "last_name": "Ford", "gpa": 83}]

>>> print data.yaml
- {age: 90, first_name: John, last_name: Adams}
- {age: 83, first_name: Henry, last_name: Ford}

>>> print data.csv

>>> open('people.xls', 'w').write(data.xls)

Excel files with multiple sheets are also supported (via the DataBook object).

To get started:

$ pip install tablib

[Source on GitHub] [PyPi Listing]

Have comments? Send a tweet to @TheChangelog on Twitter.

Subscribe to The Changelog Weekly – our weekly email covering everything that hits our open source radar.