std.csv

  • Implements functionality to read Comma Separated Values and its variants
  • from an input range of dchar.
  • Comma Separated Values provide a simple means to transfer and store
  • tabular data. It has been common for programs to use their own
  • variant of the CSV format. This parser will loosely follow the
  • RFC-4180. CSV input should adhere
  • to the following criteria (differences from RFC-4180 in parentheses):
    • A record is separated by a new line (CRLF,LF,CR)
    • A final record may end with a new line
    • A header may be provided as the first record in input
    • A record has fields separated by a comma (customizable)
    • A field containing new lines, commas, or double quotes
    • should be enclosed in double quotes (customizable)
    • Double quotes in a field are escaped with a double quote
    • Each record should contain the same number of fields
  • Example:
  • -------
  • import std.algorithm;
  • import std.array;
  • import std.csv;
  • import std.stdio;
  • import std.typecons;
  • void main()
  • {
  • auto text = "Joe,Carpenter,300000\nFred,Blacksmith,400000\r\n";
  • foreach (record; csvReader!(Tuple!(string, string, int))(text))
  • {
  • writefln("%s works as a %s and earns $%d per year",
  • record0, record1, record2);
  • }
  • // To read the same string from the file "filename.csv":
  • auto file = File("filename.csv", "r");
  • foreach (record;
  • file.byLine.joiner("\n").csvReader!(Tuple!(string, string, int)))
  • {
  • writefln("%s works as a %s and earns $%d per year",
  • record0, record1, record2);
  • } }
  • }
  • -------
  • When an input contains a header the Contents can be specified as an
  • associative array. Passing null to signify that a header is present.
  • -------
  • auto text = "Name,Occupation,Salary\r" ~
  • "Joe,Carpenter,300000\nFred,Blacksmith,400000\r\n";
  • foreach (record; csvReader!(stringstring)
  • (text, null))
  • {
  • writefln("%s works as a %s and earns $%s per year.",
  • record"Name", record"Occupation",
  • record"Salary");
  • }
  • // To read the same string from the file "filename.csv":
  • auto file = File("filename.csv", "r");
  • foreach (record; csvReader!(stringstring)
  • (file.byLine.joiner("\n"), null))
  • {
  • writefln("%s works as a %s and earns $%s per year.",
  • record"Name", record"Occupation",
  • record"Salary");
  • }
  • -------
  • This module allows content to be iterated by record stored in a struct,
  • class, associative array, or as a range of fields. Upon detection of an
  • error an CSVException is thrown (can be disabled). csvNextToken has been
  • made public to allow for attempted recovery.
  • Disabling exceptions will lift many restrictions specified above. A quote
  • can appear in a field if the field was not quoted. If in a quoted field any
  • quote by itself, not at the end of a field, will end processing for that
  • field. The field is ended when there is no input, even if the quote was not
  • closed.

    See Also

  • Comma-separated values
  • Source: std/csv.d