tap - Importing and Exporting Tabular Data

The Tab class supports importing and exporting of tabular data from various sources. Import is achieved through the load() function, export through the table’s Tab.save() method.

Example:

# load data from comma separated value file using ',' as the separator
tab = tap.load('data.csv', sep=',')
tap.load(stream_or_filename, format='auto', sep=', ')

Load table from an input stream or the file pointed to by filename.

By default, the file format is set to auto, which tries to guess the file format from the file extension. The following file extensions are recognized:

extension recognized format
.csv comma separated values
.pickle pickled byte stream
<all others> ost-specific format

For csv and pickle files with other file extensions, the format must be specified explicitly by setting format the appropriate format string.

The following file formats are understood:

  • ost

    This is an ost-specific, but still human readable file format. The file (stream) must start with header line of the form

    col_name1[type1] <col_name2[type2]>...

    The types given in brackets must be one of the data types the Tab class understands. Each following line in the file then must contains exactly the same number of data items as listed in the header. The data items are automatically converted to the column format. Lines starting with a ‘#’ and empty lines are ignored.

  • pickle

    Deserializes the table from a pickled byte stream

  • csv

    Reads the table from comma separated values stream. Since there is no explicit type information in the csv file, the column types are guessed, using the following simple rules:

    • if all values are either NA/NULL/NONE the type is set to string
    • if all non-null values are convertible to float/int the type is set to float/int
    • if all non-null values are true/false/yes/no, the value is set to bool
    • for all other cases, the column type is set to string
Returns:A new Tab instance
Tab.save(stream_or_filename, format='ost', sep=', ')

save the table to stream or file pointed to by filename. The following three file formats are supported (for more information on file formats, see load()):

ost ost-specific format (human readable)
csv comma separated values (human readable)
pickle pickled byte stream (binary)
html HTML table
context ConTeXt table
Parameters:
  • stream_or_filename (str or file) – filename or stream for writing output
  • format (str) – output format (i.e. ost, csv, pickle)
Raises :

ValueError if format is unknown

Project Versions

Previous topic

Data visualisation

This Page