CSVReader

MacroModule
genre File
authors Jan Hendrik Moltz, Hans Meine
package FMEstable/ReleaseMeVis
definition CSVReader.def
keywords CSV, file, read, parse

Purpose

Reads a CSV file, making it possible to select rows and extract column values.

Usage

Choose the filename of a CSV file. Select a row index and up to eight column indices in order to display the values of the corresponding entries. Use the “All Rows” tab for viewing all rows (and potentially sorting by a particular column).

Details

The CSV file is parsed using Python’s csv module. Some parameters of the module - Delimiter, Quote Char and Skip Initial Space - can be set. See also the section on dialects and formatting parameters in the Python documentation. Internally, the content of the file is stored in a list of lists which can be addressed by row and column indices.

Numerical columns are recognized, which enables proper numerical sorting (by clicking on column headers in the “All Rows” tab).

Tips

It is also possible to load CSV files using drag & drop.

Values from the currently selected row can not only be accessed via the field interface, but also via scripting:

row = ctx.module('CSVReader').call('getSelectedRow') # returns a list
record = ctx.module('CSVReader').call('getSelectedRecord') # returns a dict, indexed by column names

Numbers among the values will already have the appropriate Python type (int/float).

This is useful in conjunction with the fact that the listview (from the “All Rows” tab) can be embedded in other panels using the Panel MDL control:

Panel {
  module = CSVReader
  panel = listView
}

Windows

Default Panel

../../../Projects/FileReader/CSVReader/Modules/mhelp/Images/Screenshots/CSVReader._default.png

Parameter Fields

Field Index

Auto Load: Bool Delimiter: String Skip Initial Space: Bool
Column1: String Encoding: Enum trueFilename: String
Column2: String Filename: String Value1: String
Column3: String First Row Contains Header: Bool Value2: String
Column4: String Headers found (for copy-pasting into column fields): String Value3: String
Column5: String Load: Trigger Value4: String
Column6: String Number Of Columns: Integer Value5: String
Column7: String Number Of Rows: Integer Value6: String
Column8: String Quote Char: String Value7: String
dataChanged: Trigger Row Index: Integer Value8: String

Visible Fields

Filename

name: filename, type: String

Name of the CSV file. May use $(HOME) etc., as in LocalFileName.

Delimiter

name: delimiter, type: String, default: ,

Character that separates entries within a row, usually “,” or “;”.

Use “t” in order to enter a <tab> character as delimiter for TSV files (“tab-separated values”).

Quote Char

name: quoteChar, type: String, default: "

Character used for quoting entries containing the delimiter or newlines.

Skip Initial Space

name: skipInitialSpace, type: Bool, default: FALSE

Flag that enables removing spaces at the beginning of a parsed entry.

First Row Contains Header

name: firstRowContainsHeader, type: Bool, default: TRUE

Determine whether the first row contains headers (shifts Row Index by one).

Encoding

name: encoding, type: Enum, default: Default

Encoding that is used to open the .csv file.

Feel free to add other encodings, if needed.

Values:

Title Name Description
Default Default Uses the default encoding of the operating system.
utf-8 utf-8 Uses uft-8 encoding.

Load

name: load, type: Trigger

Triggers loading data from the given file.

Auto Load

name: autoLoad, type: Bool, default: TRUE

Load data automatically when the filename or CSV reading settings changes.

Number Of Rows

name: numberOfRows, type: Integer, persistent: no

Number of rows found in the CSV file.

Number Of Columns

name: numberOfColumns, type: Integer, persistent: no

Number of columns found in the CSV file (maximum from all data rows).

Row Index

name: rowIndex, type: Integer, default: 0, minimum: 0

Index of the row to be selected (starting with 0 in the first data row, potentially skipping the header).

Column1

name: column1, type: String, default: 0, deprecated name: columnIndex1

Index or name of the column to be read from the selected row into Value1.

Names are supported if First Row Contains Header is set; in the “Settings” tab, there is a Headers found (for copy-pasting into column fields) field for easier copy-pasting of names into the column fields.

Column2

name: column2, type: String, default: 1, deprecated name: columnIndex2

Analoguous to Column1.

Column3

name: column3, type: String, default: 2, deprecated name: columnIndex3

Analoguous to Column1.

Column4

name: column4, type: String, default: 3, deprecated name: columnIndex4

Analoguous to Column1.

Column5

name: column5, type: String, default: 4, deprecated name: columnIndex5

Analoguous to Column1.

Column6

name: column6, type: String, default: 5, deprecated name: columnIndex6

Analoguous to Column1.

Column7

name: column7, type: String, default: 6, deprecated name: columnIndex7

Analoguous to Column1.

Column8

name: column8, type: String, default: 7, deprecated name: columnIndex8

Analoguous to Column1.

Value1

name: value1, type: String, persistent: no

Value read from the column Column1 of the selected row (Row Index)

Value2

name: value2, type: String, persistent: no

Analoguous to Value1.

Value3

name: value3, type: String, persistent: no

Analoguous to Value1.

Value4

name: value4, type: String, persistent: no

Analoguous to Value1.

Value5

name: value5, type: String, persistent: no

Analoguous to Value1.

Value6

name: value6, type: String, persistent: no

Analoguous to Value1.

Value7

name: value7, type: String, persistent: no

Analoguous to Value1.

Value8

name: value8, type: String, persistent: no

Analoguous to Value1.

Headers found (for copy-pasting into column fields)

name: headers, type: String, persistent: no

Headers found in current file (if First Row Contains Header is set). Intended for easy copy-pasting of column names into Column1 etc.

Hidden Fields

trueFilename

name: trueFilename, type: String, persistent: no

dataChanged

name: dataChanged, type: Trigger, persistent: no

Trigger field that gets touched after the CSV file parsing is done (or if the data was cleared).

All other fields will already be up to date when this field is touched.