Rhombus CSV Reading and Writing
| import: rhombus/csv | package: rhombus-csv-lib |
The rhombus/csv module provides functions for reading and writing comma-separated value (CSV) files and tab-separated value (TSV) files.
CSV is a weakly standardized format, so csv.Reader and csv.Writer support a variety of configurations. The default csv.Reader configuration parses files that follow the RFC 4180 standard, but it will also accept variants, such as files that use a linefeed (LF) terminator instead of carriage return plus linefeed (CRLF). The default csv.Writer configuration conforms to RFC 4180.
1 CSV Reader
class | ||||||||||
|
line_mode: Configures how line terminators are recognized in the same way as for Port.Input.read_line. An end-of-file is always treated as a line terminator, too, when it follows input that does not have a terminator.
separator_chars: Configures a set of characters that are treated as field separators within a line.
quote_chars: Configures a set of characters that are treated as quoting a field when one appears at the beginning of a field.
quote_doubling_escapes: Configures whether a quote character twice in a row escapes the character within a quoted field.
newlines_in_quotes: Configures whether a quoted field can contain a newline, where the notion of a “newline” depends on line_mode. If newlines are not allowed in a quoted field, then an exception is thrown when one is encountered.
space_after_quotes: Configures whether a quoted field can be followed by one or more space characters before the end of the field (as determined by a separator character or the end of the line). An error is reported when the reader encounters extra non-space characters after a close quote or if it encountered a space character when space_after_quotes is #false.
comment_chars: Configures a set of characters that comment out a line of input when one appears at the start of the line.
trim: Configures a function for adjusting unquoted fields (but not quoted fields). For example, String.trim might be a suitable trimming function.
method | |||
| |||
| |||
method | |||
|
The csv.Reader.read_line method reads a single line of a CSV file and returns a list for a singe line, or it returns Port.eof if inp reports an immediate end-of-file. The csv.Reader.read_line method can read multiple lines of input from inp if the reader is configured to recognize comments, and in that case, the result is Port.eof is an end-of-file is encountered after comment lines.
2 CSV Writer
class | |||||||
|
newline: Configures the string used to terminate lines.
separator_char: Configures the string used to separate fields. When this character appears in a field, the field is quoted.
quote_char: Configures the string used to quote fields. When this character appears in a field, the field is quoted, and the quote character is used twice in the quoted form to represent itself as a character in the field.
extra_quote_chars: Configures additional characters that are treated as quotes, a field must be quoted (using quote_char) when it contains one of these characters, and the character is doubled within the quoted field to represent itself as a character in the field.
extra_special_chars: Configures additional characters that trigger quoting of a field when they appear in the field, but they can represent themselves (without doubling) in a quoted field.
method | ||||
| ||||
| ||||
method | ||||
|
The csv.Writer.write_line method reads a single line of a CSV file. It accepts a list for a singe line.