C
C#12mo ago
RiamuYui

❔ Trying to map CSVHelper to weirdly formed CSV

I have this CSV and I am struggling to write the class to use when reading it with CSVHelper. I am only trying to read E_Grid.
25 Replies
grimpows
grimpows12mo ago
there is delimiter or its fixed ? you can do semi-manual read of it
RiamuYui
RiamuYui12mo ago
My current mapping is this:
RiamuYui
RiamuYui12mo ago
I've never had to deal with CSVs that don't have headings in the first row
grimpows
grimpows12mo ago
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false,
Delimiter = "your_delimiter"

};
using (var reader = new StreamReader(filePath))
using (var csv = new CsvReader(reader, config))
{
int header_rows_to_skip = 13;
int row_counts = 0;
while (csv.Read())
{
row_counts++;
if(row_counts < header_rows_to_skip) continue;

string E_GridValue = csv.GetField(4);
// do something like adding it to a list of string
}

}
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false,
Delimiter = "your_delimiter"

};
using (var reader = new StreamReader(filePath))
using (var csv = new CsvReader(reader, config))
{
int header_rows_to_skip = 13;
int row_counts = 0;
while (csv.Read())
{
row_counts++;
if(row_counts < header_rows_to_skip) continue;

string E_GridValue = csv.GetField(4);
// do something like adding it to a list of string
}

}
dont use map for this case i guess do a semi-manual if data is 'fixed' like skiping the X first row you can even set the number to skip in a application param so isnt fixed in code
RiamuYui
RiamuYui12mo ago
What do I use as my delimeter?
grimpows
grimpows12mo ago
if your file is a CSV oppen it with a normal file reader and not excel you will see the delimiter (if its a real csv) usualy its ";" the ; char
RiamuYui
RiamuYui12mo ago
I have it open in notepad, I just have commas, no ;
grimpows
grimpows12mo ago
so its coma coma betwen fields ?
RiamuYui
RiamuYui12mo ago
grimpows
grimpows12mo ago
yep its coma delimiter you can just do a Console.WriteLine(csv.GetField(4)); to see different value for the 5th field (start at 0 for the parameters of GetField = 1st value)
RiamuYui
RiamuYui12mo ago
ran into "Synchronous reads are not supported" when starting the while loop
grimpows
grimpows12mo ago
hmm weird, can you send me just few part of your csv ? i may try
grimpows
grimpows12mo ago
its working for me
internal class Program
{
static void Main(string[] args)
{

var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false,
Delimiter = ","

};
using (var reader = new StreamReader(Config.FILE_PATH)) // replace by the path of your file
using (var csv = new CsvReader(reader, config))
{
int header_rows_to_skip = 14;
int row_counts = 0;
while (csv.Read())
{
row_counts++;
if (row_counts < header_rows_to_skip) continue;

string E_GridValue = csv.GetField(4);
Console.WriteLine(E_GridValue);
// do something like adding it to a list of string
}

}


Console.ReadKey();

}
}
internal class Program
{
static void Main(string[] args)
{

var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false,
Delimiter = ","

};
using (var reader = new StreamReader(Config.FILE_PATH)) // replace by the path of your file
using (var csv = new CsvReader(reader, config))
{
int header_rows_to_skip = 14;
int row_counts = 0;
while (csv.Read())
{
row_counts++;
if (row_counts < header_rows_to_skip) continue;

string E_GridValue = csv.GetField(4);
Console.WriteLine(E_GridValue);
// do something like adding it to a list of string
}

}


Console.ReadKey();

}
}
grimpows
grimpows12mo ago
grimpows
grimpows12mo ago
and to match your model i'll modify the code, but its just creating a list on starting then add the value to the list
public class PVSystModel
{
public string E_Grid { get; set; }
}

internal class Program
{
static void Main(string[] args)
{

List<PVSystModel> pVSystModels = new List<PVSystModel>();

var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false,
Delimiter = ","

};
using (var reader = new StreamReader(Config.FILE_PATH))
using (var csv = new CsvReader(reader, config))
{
int header_rows_to_skip = 14;
int row_counts = 0;
while (csv.Read())
{
row_counts++;
if (row_counts < header_rows_to_skip) continue;

string E_GridValue = csv.GetField(4);

pVSystModels.Add(new PVSystModel { E_Grid = E_GridValue });
//
// do something like adding it to a list of string
}

}


foreach (PVSystModel pvsysModel in pVSystModels)
{
Console.WriteLine(pvsysModel.E_Grid);
}

Console.ReadKey();

}
}
public class PVSystModel
{
public string E_Grid { get; set; }
}

internal class Program
{
static void Main(string[] args)
{

List<PVSystModel> pVSystModels = new List<PVSystModel>();

var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false,
Delimiter = ","

};
using (var reader = new StreamReader(Config.FILE_PATH))
using (var csv = new CsvReader(reader, config))
{
int header_rows_to_skip = 14;
int row_counts = 0;
while (csv.Read())
{
row_counts++;
if (row_counts < header_rows_to_skip) continue;

string E_GridValue = csv.GetField(4);

pVSystModels.Add(new PVSystModel { E_Grid = E_GridValue });
//
// do something like adding it to a list of string
}

}


foreach (PVSystModel pvsysModel in pVSystModels)
{
Console.WriteLine(pvsysModel.E_Grid);
}

Console.ReadKey();

}
}
RiamuYui
RiamuYui12mo ago
I think my problem is the way I'm loading the file as it's thourhg an IBrowserFile
grimpows
grimpows12mo ago
IBrowserFile ? mebbe just try download it i'm not confident with how stream work
RiamuYui
RiamuYui12mo ago
as in uploading a file into blazor
grimpows
grimpows12mo ago
almost time i'm doing what the doc say about filestream x') but for a file on the drive this work set the file on your drive and read it with the code above you will see its work
RiamuYui
RiamuYui12mo ago
Yes it does and thank you!
grimpows
grimpows12mo ago
but for wich other streamReader to use i'm afraid i will not be a good help x')
RiamuYui
RiamuYui12mo ago
no worries, thank you for all your help!
grimpows
grimpows12mo ago
np and gl with your project :p
Accord
Accord11mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.