Rhombus CSV Reading and Writing
1 CSV Reader
Reader
Reader.line_  mode
Reader.separator_  chars
Reader.quote_  chars
Reader.quote_  doubling_  escapes
Reader.newlines_  in_  quotes
Reader.space_  after_  quotes
Reader.comment_  chars
Reader.trim
Reader.read
Reader.read_  line
Reader.tsv
2 CSV Writer
Writer
Writer.newline
Writer.separator_  char
Writer.quote_  char
Writer.extra_  quote_  chars
Writer.extra_  special_  chars
Writer.write
Writer.write_  line
Writer.tsv
9.0

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

class csv.Reader(

  ~line_mode: line_mode :: Port.Input.ReadLineMode = #'any,

  ~separator_chars: separator_chars :: Set.of(Char) = Set.by(===){Char","},

  ~quote_chars: quote_chars :: Set.of(Char) = Set.by(===){Char"\""},

  ~quote_doubling_escapes: quote_doubling_escapes :: Boolean = #true,

  ~newlines_in_quotes: newlines_in_quotes :: Boolean = #true,

  ~space_after_quotes: space_after_quotes :: Boolean = #true,

  ~comment_chars: comment_chars :: Set.of(Char) = Set.by(===){},

  ~trim: trim :: (String -> String) = values

)

Creates a CSV reader (or TSV reader, depending on the configuration), where fields configure the reader’s behavior. Call the csv.Reader.tsv method to get a reader whose configuration is adjusted for tab-separated values.

  • 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 (reader :: csv.Reader).read(

  inp :: Port.Input = stdin

) :~ List.of(List.of(String))

 

method

method (reader :: csv.Reader).read_line(

  inp :: Port.Input = stdin

) :~ List.of(String) || Port.EOF

The csv.Reader.read method reads lines of a CSV file. It returns a list where each element is a line, and each line is represented by a list that has a string for each field.

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.

method

method (reader :: csv.Reader).tsv() :~ csv.Reader

Returns a csv.Reader that is like reader, but with the separator configuration changed to {Char"\t"}.

2 CSV Writer🔗ℹ

class

class csv.Writer(

  ~newline: newline :: String = "\r\n",

  ~separator_char: separator_char :: Char = Char",",

  ~quote_char: quote_char :: Char = Char"\"",

  ~extra_quote_chars: extra_quote_chars :: Set.of(Char) = Set.by(===){},

  ~extra_special_chars: extra_special_chars :: Set.of(Char) = Set.by(===){}

)

Creates a CSV writer (or TSV writer, depending on the configuration), where fields configure the writer’s behavior. Call the csv.Writer.tsv method to get a writer whose configuration is adjusted for tab-separated values.

  • 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 (writer :: csv.Writer).write(

  lines :: List.of(List.of(String)),

  ~out: outp :: Port.Output = stdout

) :~ Void

 

method

method (writer :: csv.Writer).write_line(

  line :: List.of(String),

  ~out: outp :: Port.Output = stdout

) :~ Void

The csv.Writer.write method writes lines of a CSV file. It accepts a list where each element is a line, and each line is a list that has a string for each field.

The csv.Writer.write_line method reads a single line of a CSV file. It accepts a list for a singe line.

method

method (writer :: csv.Writer).tsv() :~ csv.Writer

Returns a csv.Writer that is like writer, but with the separator configuration changed to {Char"\t"}.