C
C#2w ago
Faker

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
jcotton42
jcotton422w ago
Not built in, but plenty are on nuget. Where are the CSVs from though? @Faker
Faker
FakerOP2w ago
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?
jcotton42
jcotton422w ago
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.
Faker
FakerOP2w ago
yeah I see, this is what I read, in my case though, nothing of that applies, it's simple values, like:
A,B,C,D
E,F,G,H
A,B,C,D
E,F,G,H
So I guess I can just stick with the classic way?
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Faker
FakerOP2w ago
Ok, will have a look at them
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Faker
FakerOP2w ago
yep noted, will read their docs and understand how all of that works, thanks !
Anton
Anton2w ago
being able to make your own parser for stuff like this is very helpful though so many problems come down to parsing
Faker
FakerOP2w ago
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 ?
jcotton42
jcotton422w ago
yes
Faker
FakerOP2w ago
oh ok, thanks !
Anton
Anton2w ago
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
a
b,c
d
a
b,c
d
Faker
FakerOP2w ago
oh ok I see, that's good to know, thanks !
Anton
Anton2w ago
you're welcome

Did you find this page helpful?