C
C#16mo 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
grimpows16mo ago
there is delimiter or its fixed ? you can do semi-manual read of it
RiamuYui
RiamuYuiOP16mo ago
My current mapping is this:
RiamuYui
RiamuYuiOP16mo ago
I've never had to deal with CSVs that don't have headings in the first row
grimpows
grimpows16mo 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
RiamuYuiOP16mo ago
What do I use as my delimeter?
grimpows
grimpows16mo 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
RiamuYuiOP16mo ago
I have it open in notepad, I just have commas, no ;
grimpows
grimpows16mo ago
so its coma coma betwen fields ?
RiamuYui
RiamuYuiOP16mo ago
grimpows
grimpows16mo 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
RiamuYuiOP16mo ago
ran into "Synchronous reads are not supported" when starting the while loop
grimpows
grimpows16mo ago
hmm weird, can you send me just few part of your csv ? i may try
RiamuYui
RiamuYuiOP16mo ago
grimpows
grimpows16mo 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
grimpows16mo ago
grimpows
grimpows16mo 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
RiamuYuiOP16mo ago
I think my problem is the way I'm loading the file as it's thourhg an IBrowserFile
grimpows
grimpows16mo ago
IBrowserFile ? mebbe just try download it i'm not confident with how stream work
RiamuYui
RiamuYuiOP16mo ago
as in uploading a file into blazor
grimpows
grimpows16mo 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
RiamuYuiOP16mo ago
Yes it does and thank you!
grimpows
grimpows16mo ago
but for wich other streamReader to use i'm afraid i will not be a good help x')
RiamuYui
RiamuYuiOP16mo ago
no worries, thank you for all your help!
grimpows
grimpows16mo ago
np and gl with your project :p
Accord
Accord16mo 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.
Want results from more Discord servers?
Add your server