csvReader & specifying separator problems...

Robert M. Münch robert.muench at saphirion.com
Thu Nov 14 12:25:30 UTC 2019


Just trying a very simple thing and it's pretty hard: "Read a CSV file 
(raw_data) that has a ; separator so that I can iterate over the lines 
and access the fields."

	csv_data = raw_data.byLine.joiner("\n")

>From the docs, which I find extremly hard to understand:

auto csvReader(Contents = string, Malformed ErrorLevel = 
Malformed.throwException, Range, Separator = char)(Range input, 
Separator delimiter = ',', Separator quote = '"')

So, let's see if I can decyphre this, step-by-step by trying out:

	csv_records = csv_data.csvReader();

Would split the CSV data into iterable CSV records using ',' char as 
separator using UFCS syntax. When running this I get:

	std.csv.CSVException@/Library/D/dmd/src/phobos/std/csv.d(1283): Row 
1's length 0 does not match previous length of 1.

Which indicates some problem because not all fields are set in my CSV 
data. So let's ignore any error by specifying Malformed.ignore;

	csv_records = csv_data.csvReader(Malformed.ignore);

And now I'm lost (just showing the first candidate):

Error: template std.csv.csvReader cannot deduce function from argument 
types !()(Result, Malformed), candidates are:
/Library/D/dmd/src/phobos/std/csv.d(327):        csvReader(Contents = 
string, Malformed ErrorLevel = Malformed.throwException, Range, 
Separator = char)(Range input, Separator delimiter = ',', Separator 
quote = '"')
  with Contents = string,
       ErrorLevel = cast(Malformed)1,
       Range = Result,
       Separator = Malformed
  whose parameters have the following constraints:
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    isInputRange!Range
    is(Unqual!(ElementType!Range) == dchar)
  > isSomeChar!Separator
  - !is(Contents T : T[U], U : string)
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The docs state Malformed as 2nd parameter, since I use UFCS I assume 
that this becomes the first parameter. I don't understand what the 3rd 
parameter (Range) is about. 4th parameter is my separator, which I need 
to set to ';' somehow.

But from the error message, it looks like DMD tries to use 
Malformed.ignore as the 4th (!!) Parameter being the Separator.

I'm totally confused:

* What is used as the 3rd parameter by DMD? Where does it come from?
* How to specify a ';' separator?

This is all pretty confusing...

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



More information about the Digitalmars-d-learn mailing list