Skip to main content

Iterable API for tabular datasets including CSV, XLS, XML, & JSON.

Project description

IterTable is a Pythonic API for iterating through tabular data formats, including CSV, XLS, XML, and JSON.

from itertable import load_file

for row in load_file("example.xls"):
    print(row.date, row.name)

Latest PyPI Release Release Notes License GitHub Stars GitHub Forks GitHub Issues

Travis Build Status Python Support

Note: Prior to version 2.0, IterTable was wq.io, a submodule of the wq framework. The package has been renamed to avoid confusion with the wq framework website (https://wq.io). Similarly, IterTable's *IO classes have been renamed to *Iter, as the API is not intended to match that of Python's StringIO or other io classes.

- from wq.io import CsvFileIO
- data = CsvFileIO(filename='data.csv')
+ from itertable import CsvFileIter
+ data = CsvFileIter(filename='data.csv')

Getting Started

# Recommended: create virtual environment
# python3 -m venv venv
# . venv/bin/activate

python3 -m pip install itertable

# GIS support (Fiona & Shapely)
python3 -m pip install itertable[gis]

# Excel write support
python3 -m pip install itertable[write]
# (xls/xlsx read support is enabled by default)

# Pandas integration
python3 -m pip install itertable[pandas]

Overview

IterTable provides a general purpose API for loading, iterating over, and writing tabular datasets. The goal is to avoid needing to remember the unique usage of e.g. csv, xlrd, or xml.etree every time one needs to work with external data. Instead, IterTable abstracts these libraries into a consistent interface that works as an iterable of namedtuples. Whenever possible, the field names for a dataset are automatically determined from the source file, e.g. the column headers in an Excel spreadsheet.

from itertable import ExcelFileIter
data = ExcelFileIter(filename='example.xls')
for row in data:
    print(row.name, row.date)

IterTable provides a number of built-in classes like the above, including a CsvFileIter, XmlFileIter, and JsonFileIter. There is also a convenience function, load_file(), that attempts to automatically determine which class to use for a given file.

from itertable import load_file
data = load_file('example.csv')
for row in data:
    print(row.name, row.date)

All of the included *FileIter classes support both reading and writing to external files, though write support for Excel files requires itertable[write] (which installs xlwt and xlswriter).

Network Client

IterTable also provides network-capable equivalents of each of the above classes, to facilitate loading data from third party webservices.

from itertable import JsonNetIter
class WebServiceIter(JsonNetIter):
    url = "http://example.com/api"

data = WebServiceIter(params={'type': 'all'})
for row in data:
    print(row.timestamp, row.value)

The powerful requests library is used internally to load data over HTTP.

Pandas Analysis

When Pandas is installed (via itertable[pandas]), the as_dataframe() method on itertable classes can be used to create a DataFrame, enabling more extensive analysis possibilities.

instance = WebServiceIter(params={'type': 'all'})
df = instance.as_dataframe()
print(df.value.mean())

GIS Support

When Fiona and Shapely are installed (via itertable[gis]), itertable can also open and create shapefiles and other OGR-compatible geographic data formats.

from itertable import ShapeIter
data = ShapeIter(filename='sites.shp')
for id, site in data.items():
    print(id, site.geometry.wkt)

More information on IterTable's gis support is available here.

Command-Line Interface

IterTable provides a simple CLI for rendering the content of a file or Iter class. This can be useful for e.g. inspecting a file or for integrating a shell automation workflow. The default output is CSV, but can be changed to JSON by setting -f json.

python3 -m itertable example.json         # JSON to CSV
python3 -m itertable -f json example.csv  # CSV to JSON
python3 -m itertable example.xlsx "start_row=5"
python3 -m itertable http://example.com/example.csv
python3 -m itertable itertable.CsvNetIter "url=http://example.com/example.csv"

Extending IterTable

It is straightforward to extend IterTable to support arbitrary formats. Each provided class is composed of a BaseIter class and mixin classes (loaders, parsers, and mappers) that handle the various steps of the process.

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

itertable-2.0.0.tar.gz (46.4 kB view details)

Uploaded Source

Built Distribution

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

itertable-2.0.0-py3-none-any.whl (21.1 kB view details)

Uploaded Python 3

File details

Details for the file itertable-2.0.0.tar.gz.

File metadata

  • Download URL: itertable-2.0.0.tar.gz
  • Upload date:
  • Size: 46.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.2

File hashes

Hashes for itertable-2.0.0.tar.gz
Algorithm Hash digest
SHA256 3835bd8e90161505a430adf37f98c70db98287d05f4d1d87ae9f8936dc309224
MD5 41a7c8b98540538084fc47142ac954fd
BLAKE2b-256 23478b90d0a295aaffa97d0ae557f4406eeeb62fcc19ef9bad2766a66f60b235

See more details on using hashes here.

File details

Details for the file itertable-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: itertable-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 21.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.2

File hashes

Hashes for itertable-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b26fde7c4ad9482f75594e73a3a57feb1560122736572290dc4b8fab50fe248c
MD5 801d382d8d25a328c58bf689cb179ee7
BLAKE2b-256 8c6936f75760e71b6f094e5713e3f803a467d026e0aa411ed8e5d5bfa179faf6

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