CSVLoader class

Load a CSV file. This class can:

  • Define some fields (columns) as mandatory;
  • Define, per field, how to transform its value from string to something else
  • Handle case sensitive/insensitive field names

You can define CSV fields using add_field() , in which case you can also specify their type and whether they’re optional or not.

Excessive fields are allowed too, they will be loaded as strings.

Methods and properties:

class trajtracker.io.CSVLoader(case_sensitive_col_names=False)
FLD_LINE_NUM = 'line_num'

Each loaded row will have an auto-generated field with this name. The field contains the corresponding line number in the csv text file (int)

__init__(case_sensitive_col_names=False)

Create a CSVLoader object

Parameters:case_sensitive_col_names – If True, all field names are case insensitive (when converting each row to dict, the dict key will be lowercase)
add_field(field_name, field_type=<type 'str'>, optional=False)

Define a configurable field. You do not have to define all fields in the file - only those for which you need special configuration.

Do this before calling load_file()

Parameters:
  • field_type – either a type, or a function that converts the string (from the CSV file) to another value
  • optional – If False, load_file() will fail if the field is missing.
load_file(filename)

Load data from the CSV file

Returns:a tuple with two elements: (1) list with one dict per row, transformed to the required types; (2) List of field names that were found in the file.
log_level

Logging level of this object: trajtracker.log_none, log_error (default), log_warn, log_info, log_debug, log_trace