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:
Nonebecomes 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
quoteAllis 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
formulaHackis 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.ConfigFormat settings for CSV exports.
- format: FormatConfig¶
CSV format settings.
- class gws.plugin.csv_helper.Format(*args, **kwargs)¶
Bases:
gws.DataCSV format settings used by the writer.
- class gws.plugin.csv_helper.FormatConfig(*args, **kwargs)¶
Bases:
gws.ConfigCSV format settings.
- class gws.plugin.csv_helper.Object¶
Bases:
gws.NodeCSV helper.
- 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.