std/csv
On this page
Index
- type
CsvErr- method
CsvErr.display
- method
- type
Reader- method
Reader.read
- method
- fn
parse - fn
reader - fn
parse_maps - fn
format - fn
valid_sep - fn
valid_comment
Types
CsvErr
struct CsvErr {
start_line: Int
line: Int
column: Int
msg: Str
}Where and why the text is not valid CSV (Go’s csv.ParseError):
the line the record starts on, the line and 1-based byte column of the
error, and Go’s message (bare " in non-quoted-field, extraneous or missing " in quoted-field, wrong number of fields). Displays as Go’s
Error(): parse error on line 1, column 5: bare " in non-quoted-field,
record on line 2; parse error on line 3, column 1: ..., record on line 4: wrong number of fields.
CsvErr.display
fn (e: CsvErr) display() -> StrReader
struct Reader {}Records read one at a time from an open file (Go’s csv.NewReader(file),
Read), the options as parse’s: the file is read as the records are
asked for, so a file larger than memory can be walked. A quoted field
may hold line breaks.
Reader.read
fn (var r: Reader) read() -> List[Str]? ! CsvErr uses fs.readThe next record, None at the end of the file. A parse error is as
parse’s; a failure of the file is a CsvErr with line 0 and the
file’s error text as msg.
Functions
parse
fn parse(text: Str, sep: Rune = ',', comment: Rune = '\0', fields: Int = -1, lazy_quotes: Bool = false, trim_leading_space: Bool = false) -> List[List[Str]] ! CsvErrThe records of text, as Go’s csv.Reader reads them (decision 0298):
sep separates fields (Comma), lines starting with comment are
skipped (Comment; '\0', the default, for none), fields is FieldsPerRecord (-1 any number, the
default here; 0 the first record’s; N exactly N), lazy_quotes
and trim_leading_space are LazyQuotes and TrimLeadingSpace.
reader
fn reader(f: File, sep: Rune = ',', comment: Rune = '\0', fields: Int = -1, lazy_quotes: Bool = false, trim_leading_space: Bool = false) -> Readerparse_maps
fn parse_maps(text: Str, sep: Rune = ',', comment: Rune = '\0', lazy_quotes: Bool = false, trim_leading_space: Bool = false) -> List[Map[Str, Str]] ! CsvErrThe records after the first, each a map from the first record’s
fields (the header) to its own; every record must have the header’s
number of fields (wrong number of fields).
format
fn format(rows: List[List[Str]], sep: Rune = ',', crlf: Bool = false) -> StrThe text of rows as Go’s csv.Writer writes them (crlf: UseCRLF): a
field is quoted when it holds sep, a quote, \r, or \n, starts
with a space, or is \.; also when it is the only, empty field of a
record (so it reads back as a record, where Go writes an empty line).
valid_sep
fn valid_sep(r: Rune) -> BoolGo’s validDelim (reader.go): whether r can be the sep of parse,
parse_maps and format. False for '\0', ", \r, \n and U+FFFD,
and for a code point that is not a Unicode scalar value (which no Rune
holds: Rune(n) faults on it); every other code point works, a multi
byte one included.
valid_comment
fn valid_comment(r: Rune) -> BoolGo’s validDelim for comment, and true for '\0', Go’s zero value and
the default (no comment). A comment must also differ from sep:
parse, parse_maps and format fault
csv: invalid field or comment delimiter when it does not.