Reading CSV file in C#
Hello guys, quick question. I need to read a csv file. Is there any recommended way of doing it or I just stick with the classic way of reading the file line by line and spliting them by the common delimiter?
I read that their are in-built libraries for CSV, should I use them ?
15 Replies
Not built in, but plenty are on nuget.
Where are the CSVs from though? @Faker
hmm I've written them myself
oh ok, it's just for simple tasks, do I need to find the right library to download then use it? or I can just stick with StreamReader then split by the delimiter?
The big potential headaches in CSV are quote handling, the delimiter being part of a value, and newlines in values.
If you know none of those apply, you can just do a basic split.
If those do matter, consider using a library.
yeah I see, this is what I read, in my case though, nothing of that applies, it's simple values, like:
So I guess I can just stick with the classic way?
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Ok, will have a look at them
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
yep noted, will read their docs and understand how all of that works, thanks !
being able to make your own parser for stuff like this is very helpful though
so many problems come down to parsing
yeah, I should try to make my own library one day, seems interesting
small question, when it comes to csv files, normally, common delimiter is a comma, is this delimiter changeable? like can we write something like
A;B;C
and named it as *.csv
?yes
oh ok, thanks !
afaik there's no spec for this, but it is a thing.
also, having trailing delimiters is sometimes allowed. quoting parts in between the delimiters to allow delimiter characters in the value is also a thing
like having
a,"b,c",d
parse as the three strings
oh ok I see, that's good to know, thanks !
you're welcome