I am trying to read a *.csv
-file.
The *.csv
-file consist of two columns separated by semicolon (";").
I am able to read the *.csv
-file using StreamReader and able to separate each line by using the Split()
function. I want to store each column into a separate array and then display it.
Is it possible to do that?
The open-source Angara.Table library allows to load CSV into typed columns, so you can get the arrays from the columns. Each column can be indexed both by name or index. See http://predictionmachines.github.io/Angara.Table/saveload.html.
The library follows RFC4180 for CSV; it enables type inference and multiline strings.
Example:
You can see a column type using the type Column, e.g.
Since the library is focused on F#, you might need to add a reference to the FSharp.Core 4.4 assembly; click 'Add Reference' on the project and choose FSharp.Core 4.4 under "Assemblies" -> "Extensions".
Here is my variation of the top voted answer:
The
csv
variable can then be used as in the following example:Here's a special case where one of data field has semicolon (";") as part of it's data in that case most of answers above will fail.
Solution it that case will be
Just came across this library: https://github.com/JoshClose/CsvHelper
Very intuitive and easy to use. Has a nuget package too which made is quick to implement: http://nuget.org/packages/CsvHelper/1.17.0. Also appears to be actively maintained which I like.
Configuring it to use a semi-colon is easy: https://github.com/JoshClose/CsvHelper/wiki/Custom-Configurations
I am just student working on my master's thesis, but this is the way I solved it and it worked well for me. First you select your file from directory (only in csv format) and then you put the data into the lists.
I have been using csvreader.com(paid component) for years, and I have never had a problem. It is solid, small and fast, but you do have to pay for it. You can set the delimiter to whatever you like.