C
C#•7mo 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•7mo ago
$code
MODiX
MODiX•7mo 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•7mo ago
so, what issues are you having?
Chef Boyarcaese
Chef BoyarcaeseOP•7mo 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•7mo 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 BoyarcaeseOP•7mo ago
Yes yeah i fixed that part
Pobiega
Pobiega•7mo ago
ah so, the constructor for CsvReader that takes a StreamReader also takes a culture
Chef Boyarcaese
Chef BoyarcaeseOP•7mo ago
sorry i dont know what a culture is haha
Pobiega
Pobiega•7mo 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 BoyarcaeseOP•7mo ago
ohh okay so do i directly state the path in streamreader instead of storing that path in a variable?
Pobiega
Pobiega•7mo 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 BoyarcaeseOP•7mo ago
ahh i see thanks
Pobiega
Pobiega•7mo ago
and don't forget to add using to your csv reader that must also be disposed
Chef Boyarcaese
Chef BoyarcaeseOP•7mo ago
what does the invariant culture mean?
Pobiega
Pobiega•7mo 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 BoyarcaeseOP•7mo ago
ohh right that makes sense
Pobiega
Pobiega•7mo 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 BoyarcaeseOP•7mo ago
okay yeah thanks where can I learn more about these functions?
Pobiega
Pobiega•7mo ago
what functions? CsvReader?
Chef Boyarcaese
Chef BoyarcaeseOP•7mo ago
yeah stuff like that and also StreamReader
Pobiega
Pobiega•7mo ago
I'd recommend the official documentation for CsvHelper: https://joshclose.github.io/CsvHelper/getting-started/
Chef Boyarcaese
Chef BoyarcaeseOP•7mo ago
Thank you
Chef Boyarcaese
Chef BoyarcaeseOP•7mo ago
yeah theyre a bit hard to read haha thanks though
Pobiega
Pobiega•7mo ago
they really aint once you get used to it compared to most other languages, dotnets documentation is amazing 😛
Chef Boyarcaese
Chef BoyarcaeseOP•7mo ago
okay ill try reading those
Want results from more Discord servers?
Add your server