ahkab.csvlib

The csvlib module contains common routines for handling Comma Separated Values (CSV) or Tab Separated Values (TSV) files.

Functions:

  1. CSV write/load:
  1. MISC utilities

The separator can be selected setting:

csvlib.SEPARATOR = '\t' # default value
get_headers(filename)[source]

Reads the signals inside a file.

The order of the signals in the list corresponds to the order of the signals in the file.

Parameters:

filename : string
the path to the file from which the header is to be read

Returns:

headers : list of strings.

get_headers_index(headers, load_headers=None, verbose=3)[source]

Creates a list of integers. Each element in the list is the COLUMN index of the signal according to the supplied headers.

Parameters:

headers : list of strings,
the signal names, as returned by get_headers().
load_headers : list, optional
The headers for the data to be loaded. If not provided, all indeces will be returned.

Returns:

the header indeces : a list of int.

load_csv(filename, load_headers=None, nsamples=None, skip=0, verbose=3)[source]

Reads data in CVS format from filename.

Supports:

  • selective signal loading,
  • loading up to a certain number of samples,
  • skipping to a certain line, to allow incremental reading of big files.

Parameters:

filename : string
the path to the file to be read.
load_headers : list of strings, optional
Each one being a signal to be loaded. An empty list (or None) is interpreted as “read all signals”.
nsamples : int, optional
The number of samples to be read for each signal. If None, read all available samples.
skip : int, optional
The index of the first sample to be read. Default: 0

Returns:

data : ndarray
The data, ordered according to the order of load_headers (or the order on file if load_headers was empty),
headers : list of strings
the names of the signals read from file,
pos : int
position of the last sample read +1, referred to the sample #0 in the file.
EOF : bool
A flag set to true is all the data in the file were read.
write_csv(filename, data, headers, append=False)[source]

Writes data in CVS format to filename.

The headers have to be ordered according to the data order.

Parameters:

filename : string
the path to the file to be written. Use ‘stdout’ to write to stdout
data : ndarray
The data to be written. Notice that variables are swept across rows, time samples are swept along columns. Or equivalently: data[variable_index, sample_number]
headers : list of strings
the signal names, ordered so that headers[i] corresponds to data[i, :].
append : bool, optional
If False, the file (if it exists) will be rewritten, otherwise it will be appended to.
write_headers(filename, headers)[source]

Writes headers in CVS format to filename.

Parameters:

filename : string
the path to the file to be written. Use ‘stdout’ to write to stdout.
headers : a list of strings
the signal names, ordered.