Skip to main content

CSVDocument

Represents a CSV document that can be loaded from a file and queried.

CSVDocument provides comprehensive access to CSV data, allowing retrieval of rows, columns, and individual cells by name or index. It supports various CSV formats with configurable separators, quotes, and line endings. Data can be accessed as strings or converted using custom converter functions.

Example

var doc = CSVDocument("data.csv")
print("Columns: ${doc.get_column_count()}")
print("Rows: ${doc.get_row_count()}")
var names = doc.get_column("Name")
var first_row = doc.get_row(0)

Constructors

CSVDocument(const string file_path) -> CSVDocument

Load a CSV File.

Parameters

  • file_path: The path to the CSV file.

Example

// Read a CSV file
var doc = CSVDocument("data.csv")
print("Columns: ${doc.get_column_count()}")
print("Rows: ${doc.get_row_count()}")
var names = doc.get_column("Name")
var first_row = doc.get_row(0)

CSVDocument(const string file_path, const Map options) -> CSVDocument

Load a CSV File.

Parameters

  • file_path: The path to the CSV file.
  • options: A Map of options to use when parsing the the CSV document.
    • label_column_index (integer): Specifies the zero-based row index of the column labels. Setting it to -1 prevents column lookup by label name, and gives access to all rows as document data. Default: 0
    • label_row_index (integer): Specifies the zero-based column index of the row labels. Setting it to -1 prevents row lookup by label name, and gives access to all columns as document data. Default: -1
    • separator (string): Specifies the column separator. Default: ","
    • trim (bool): Specifies whether to trim leading and trailing spaces from cells read. Default: false
    • quoted_linebreaks (bool): Specifies whether to allow line breaks in quoted text. Default: false
    • auto_quote (bool): Specifies whether to automatically dequote data during read. Default: true
    • quote_char (string): Specifies the quote character. Default: "
    • comment_prefix (string): Specifies which prefix character to indicate a comment line. Default: "#"
    • skip_comment_lines (bool): Specifies whether to skip lines prefixed with comment_prefix. Default: false
    • skip_empty_lines (bool): Specifies whether to skip empty lines. Default: false

Example

var doc = CSVDocument("data.csv", ["label_row_index": 0, "separator": ";", "trim": true])

Members

NameDescription
get_cellReturns a cell value converted by a converter function.
get_columnReturns a column as a list of strings.
get_column_countReturns the number of columns in the CSV document.
get_column_indexReturns the index of a column by its name.
get_column_nameReturns the name of a column by its index.
get_column_namesReturns the column names.
get_rowReturns a row as a list of values converted by a converter function.
get_row_countReturns the number of rows in the CSV document.
get_row_indexReturns the index of a row by its name.
get_row_nameReturns the name of a row by its index.
get_row_namesReturns the row names.