spinetoolbox.spine_io.importers.csv_reader

Contains CSVConnector class and a help function.

author:
  1. Vennström (VTT)
date:

1.6.2019

Module Contents

spinetoolbox.spine_io.importers.csv_reader.select_csv_file(parent=None)[source]

Launches QFileDialog with no filter

class spinetoolbox.spine_io.importers.csv_reader.CSVConnector[source]

Bases: spinetoolbox.spine_io.io_api.SourceConnection

Template class to read data from another QThread.

DISPLAY_NAME = Text/CSV[source]

“Text/CSV

Type:name of data source, ex
_ENCODINGS = ['utf-8', 'utf-16', 'utf-32', 'ascii', 'iso-8859-1', 'iso-8859-2'][source]

List of available text encodings

OPTIONS[source]

dict with option specification for source.

SELECT_SOURCE_UI[source]

Modal widget that returns source object and action (OK, CANCEL)

connect_to_source(self, source)[source]

saves filepath

Parameters:source (str) – filepath
disconnect(self)[source]

Disconnect from connected source.

get_tables(self)[source]

Returns a mapping from file name to options.

Returns:dict
static parse_options(options)[source]

Parses options dict to dialect and quotechar options for csv.reader

Parameters:options (dict) – dict with options: “encoding”: file text encoding “delimiter”: file delimiter “quotechar”: file quotechar “has_header”: if first row should be treated as a header “skip”: how many rows should be skipped
Returns:
tuple dialect for csv.reader,
quotechar for csv.reader and number of rows to skip
Return type:tuple(dict, bool, integer)
file_iterator(self, options, max_rows)[source]

creates an iterator that reads max_rows number of rows from text file

Parameters:
  • options (dict) – dict with options:
  • max_rows (integer) – max number of rows to read, if -1 then read all rows
Returns:

iterator of csv file

Return type:

iterator

get_data_iterator(self, table, options, max_rows=-1)[source]

Creates an iterator for the file in self.filename

Parameters:
  • table (string) – ignored, used in abstract IOWorker class
  • options (dict) – dict with options
Keyword Arguments:
 

max_rows (int) – how many rows of data to read, if -1 read all rows (default: {-1})

Returns:

Return type:

tuple