C
C#•4w ago
Chef Boyarcaese

CSV Reader Issue

public class FlashcardService { // Using a library called CsvHelper to store and read flashcards from the CSV file private readonly string _filePath = Path.Combine(Environment.CurrentDirectory, "Data", "flashcards.csv"); public List<Flashcard> LoadFlashcards() { // CsvReader allows for opening and reading the CSV file using (var reader = new StreamReader(_filePath)) // Open file for reading { // Create a CsvReader instance using the StreamReader var csvReader = new CsvReader((reader); // Reads all records from the CSV and maps them to Flashcard objects return csvReader.GetRecords<Flashcard>().ToList(); } }
26 Replies
Pobiega
Pobiega•4w ago
$code
MODiX
MODiX•4w ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Pobiega
Pobiega•4w ago
so, what issues are you having?
Chef Boyarcaese
Chef Boyarcaese•4w ago
Im trying to read from a csv file using StreamReader but I keep recieving the error: FlashcardService.cs(16, 44): [CS1503] Argument 1: cannot convert from 'System.IO.StreamReader' to 'CsvHelper.IParser'
Pobiega
Pobiega•4w ago
hm, would that be on var csvReader = new CsvReader((reader) perhaps? also, note that your number of parenthesis on that line are unbalanaced
Chef Boyarcaese
Chef Boyarcaese•4w ago
Yes yeah i fixed that part
Pobiega
Pobiega•4w ago
ah so, the constructor for CsvReader that takes a StreamReader also takes a culture
Chef Boyarcaese
Chef Boyarcaese•4w ago
sorry i dont know what a culture is haha
Pobiega
Pobiega•4w ago
something like...
using var reader = new StreamReader(File.OpenRead("data.csv"));
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
using var reader = new StreamReader(File.OpenRead("data.csv"));
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
Chef Boyarcaese
Chef Boyarcaese•4w ago
ohh okay so do i directly state the path in streamreader instead of storing that path in a variable?
Pobiega
Pobiega•4w ago
that doesnt matter look at the second row oh and prefer the shorter using syntax that I use here over the body one you used
Chef Boyarcaese
Chef Boyarcaese•4w ago
ahh i see thanks
Pobiega
Pobiega•4w ago
and don't forget to add using to your csv reader that must also be disposed
Chef Boyarcaese
Chef Boyarcaese•4w ago
what does the invariant culture mean?
Pobiega
Pobiega•4w ago
so a Culture is an object that describes how to handle regional differences to things such as number separators etc like, in the US they use 5.11 for numbers but in sweden, we do 5,11
Chef Boyarcaese
Chef Boyarcaese•4w ago
ohh right that makes sense
Pobiega
Pobiega•4w ago
so CSV helper lets you specify a culture the "InvariantCulture" is a fixed culture that mostly resembles the US standards its good for data files since for data, even us crazy swedes prefer 5.11:D
Chef Boyarcaese
Chef Boyarcaese•4w ago
okay yeah thanks where can I learn more about these functions?
Pobiega
Pobiega•4w ago
what functions? CsvReader?
Chef Boyarcaese
Chef Boyarcaese•4w ago
yeah stuff like that and also StreamReader
Pobiega
Pobiega•4w ago
I'd recommend the official documentation for CsvHelper: https://joshclose.github.io/CsvHelper/getting-started/
Chef Boyarcaese
Chef Boyarcaese•4w ago
Thank you
Chef Boyarcaese
Chef Boyarcaese•4w ago
yeah theyre a bit hard to read haha thanks though
Pobiega
Pobiega•4w ago
they really aint once you get used to it compared to most other languages, dotnets documentation is amazing 😛
Chef Boyarcaese
Chef Boyarcaese•4w ago
okay ill try reading those