-
Notifications
You must be signed in to change notification settings - Fork 2k
Please add an example CsvWriter Class. #663
Comments
Hi there, Thanks for opening the issue! May I ask how you would like to use the CsvWriter? For example, are you trying to write matrices, jagged arrays, DataTables, or other specific types to .csv files? I am just asking so I can craft a relevant example for your intended usage. Regards, |
Thanks for your reply. I wrote the code below, but how do you "CsvWriter"?
Is there still a fix for the code "CsvReader" below?
|
Hi there, Here is an example on how to write a matrix to a .csv file: string filename = "matrix.csv";
double[,] values =
{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
};
using (CsvWriter writer = new CsvWriter(filename))
{
writer.WriteHeaders("a", "b", "c", "d"); // this is optional, but helps readability afterwards
writer.Write(values);
} and here is how to read it back using CsvReader: CsvReader reader = new CsvReader(filename, hasHeaders: true);
double[,] actual = reader.ToMatrix(); It also work with jagged arrays (double[][]) if you replace ToMatrix with ToJagged: CsvReader reader = new CsvReader(filename, hasHeaders: true);
double[][] actual = reader.ToJagged(); |
If you are storing the output labels as the last column of your csv file, you can use the following code to retrieve it: double[][] inputs = actual.Get(null, 0, -1); // null means 'all rows', and 0 to -1 means from first column to the one before the last
int[] outputs = actual.GetColumn(-1).ToInt32(); // will get the last column and convert to int |
Added in release 3.6.0. |
Or is there the examples?
The text was updated successfully, but these errors were encountered: