gws.plugin.csv_helper

CSV helper.

The csv helper writes CSV data with configurable formatting, for example for the ALKIS export. writer creates a writer, which writes headers and rows either into memory or directly into a binary stream.

Values are formatted according to their type:

  • None becomes an empty quoted string,

  • integers are written as they are; floats and decimals are formatted with the number formatter of the locale. Numbers are only quoted if quoteAll is set,

  • dates, datetimes and times are formatted in the short format of the locale and quoted,

  • other values are converted to strings and quoted. If formulaHack is set, digit-only strings are written as formulas (="0123"), so that spreadsheet programs keep leading zeros.

The helper is created with default settings if it is not configured.

Example:

helpers+ {
    type "csv"
    format {
        delimiter ";"
        encoding "cp1252"
        rowDelimiter "CRLF"
    }
}

Usage in Python:

helper = cast(gws.plugin.csv_helper.Object, root.app.helper('csv'))
w = helper.writer(gws.lib.intl.locale('de_DE'))
w.write_headers(['name', 'area'])
w.write_row(['Parcel 1', 123.4])
data = w.to_bytes()

Source code: gws.plugin.csv_helper

Package Contents

class gws.plugin.csv_helper.Config(*args, **kwargs)

Bases: gws.Config

Format settings for CSV exports.

format: FormatConfig

CSV format settings.

class gws.plugin.csv_helper.Format(*args, **kwargs)

Bases: gws.Data

CSV format settings used by the writer.

delimiter: str

Field delimiter.

encoding: str

Text encoding.

formulaHack: bool

Write digit-only strings as formulas.

quote: str

Quote character.

quoteAll: bool

Quote all fields, including numbers.

rowDelimiter: str

Row delimiter, with CR and LF replaced by the actual characters.

class gws.plugin.csv_helper.FormatConfig(*args, **kwargs)

Bases: gws.Config

CSV format settings.

delimiter: str = ','

Field delimiter.

encoding: str = 'utf8'

Text encoding.

formulaHack: bool = True

Write digit-only strings as formulas.

quote: str = '"'

Quote character.

quoteAll: bool = False

Quote all fields.

rowDelimiter: str = 'LF'

Row delimiter.

class gws.plugin.csv_helper.Object

Bases: gws.Node

CSV helper.

format: Format

Format settings.

configure() → None

Configuration hook.

Called when the node is initialized. Raising an exception here aborts the creation of the node.

writer(locale: gws.Locale, stream_to: BinaryIO = None) → _Writer

Create a CSV writer.

Parameters:
  • locale – Locale for formatting numbers, dates and times.

  • stream_to – Binary stream to write to. If None, the data is kept in memory.

Returns:

A new writer with the format settings of this helper.