View source code Display the source code in std/csv.d from which this page was generated on github. Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

std.csv.csv_reader - multiple declarations

Function csvReader

Returns an input range for iterating over records found in input.

The Contents of the input can be provided if all the records are the same type such as all integer data:

string str = 76,26,22;
int[] ans = [76,26,22];
auto records = csvReader!int(str);

foreach(record; records)
{
    assert(equal(record, ans));
}

Example using a struct with modified delimiter:

string str = "Hello;65;63.63\nWorld;123;3673.562";
struct Layout
{
    string name;
    int value;
    double other;
}

auto records = csvReader!Layout(str,';');

foreach(record; records)
{
    writeln(record.name);
    writeln(record.value);
    writeln(record.other);
}

Specifying ErrorLevel as Malformed.ignore will lift restrictions on the format. This example shows that an exception is not thrown when finding a quote in a field not quoted.

string str = "A \" is now part of the data";
auto records = csvReader!(string,Malformed.ignore)(str);
auto record = records.front;

assert(record.front == str);

Prototype

auto csvReader(Contents, std.csv.Malformed ErrorLevel, Range, Separator)(
  Range input,
  Separator delimiter = ',',
  Separator quote = '"'
)
if (isInputRange!Range && is(Unqual!(ElementType!Range) == dchar) && isSomeChar!Separator && !is(Contents T : T[U], U : string));

Returns

An input range R as defined by std.range.primitives.isInputRange. When Contents is a struct, class, or an associative array, the element type of R is Contents, otherwise the element type of R is itself a range with element type Contents.

Throws

CSVException When a quote is found in an unquoted field, data continues after a closing quote, the quoted field was not closed before data was empty, a conversion failed, or when the row's length does not match the previous length.

HeaderMismatchException when a header is provided but a matching column is not found or the order did not match that found in the input. Read the exception documentation for specific details of when the exception is thrown for different types of Contents.

Function csvReader

An optional header can be provided. The first record will be read in as the header. If Contents is a struct then the header provided is expected to correspond to the fields in the struct. When Contents is not a type which can contain the entire record, the header must be provided in the same order as the input or an exception is thrown.

Read only column "b":

string str = "a,b,c\nHello,65,63.63\nWorld,123,3673.562";
auto records = csvReader!int(str, ["b"]);

auto ans = [[65],[123]];
foreach(record; records)
{
    assert(equal(record, ans.front));
    ans.popFront();
}

Read from header of different order:

string str = "a,b,c\nHello,65,63.63\nWorld,123,3673.562";
struct Layout
{
    int value;
    double other;
    string name;
}

auto records = csvReader!Layout(str, ["b","c","a"]);

The header can also be left empty if the input contains a header but all columns should be iterated. The header from the input can always be accessed from the header field.

string str = "a,b,c\nHello,65,63.63\nWorld,123,3673.562";
auto records = csvReader(str, null);

assert(records.header == ["a","b","c"]);

Prototype

auto csvReader(Contents, std.csv.Malformed ErrorLevel, Range, Header, Separator)(
  Range input,
  Header header,
  Separator delimiter = ',',
  Separator quote = '"'
)
if (isInputRange!Range && is(Unqual!(ElementType!Range) == dchar) && isSomeChar!Separator && isForwardRange!Header && isSomeString!(ElementType!Header));

Returns

An input range R as defined by std.range.primitives.isInputRange. When Contents is a struct, class, or an associative array, the element type of R is Contents, otherwise the element type of R is itself a range with element type Contents.

The returned range provides a header field for accessing the header from the input in array form.

string str = "a,b,c\nHello,65,63.63";
auto records = csvReader(str, ["a"]);

assert(records.header == ["a","b","c"]);

Throws

CSVException When a quote is found in an unquoted field, data continues after a closing quote, the quoted field was not closed before data was empty, a conversion failed, or when the row's length does not match the previous length.

HeaderMismatchException when a header is provided but a matching column is not found or the order did not match that found in the input. Read the exception documentation for specific details of when the exception is thrown for different types of Contents.

Function csvReader

Prototype

auto csvReader(Contents, std.csv.Malformed ErrorLevel, Range, Header, Separator)(
  Range input,
  Header header,
  Separator delimiter = ',',
  Separator quote = '"'
)
if (isInputRange!Range && is(Unqual!(ElementType!Range) == dchar) && isSomeChar!Separator && is(Header : typeof(null)));

Example

import std.algorithm;

string str = 76;^26^;22;
int[] ans = [76,26,22];
auto records = CsvReader!(int,Malformed.ignore,string,char,string[])
      (str, ';', '^');

foreach(record; records)
{
    assert(equal(record, ans));
}

Authors

Jesse Phillips

License

Boost License 1.0.

Comments