Trishen
Trishen

Reputation: 237

Read a specific column from a csv file in C#

I have a simple 3 column csv file and i need to exctract only the information from the first column. I was thinking of regular expressions, but i am hoping there is an easier more efficient approach.

Any help will be much appreciated.

Thanks

Upvotes: 2

Views: 19246

Answers (6)

Josh Close
Josh Close

Reputation: 23403

If you want to extract that data into a class object, CsvHelper (a library I maintain) is a good option.

var csv = new CsvHelper( File.OpenRead( "file.csv" ) );
var myCustomObjects = csv.Reader.GetRecords<MyCustomObject>();

Upvotes: 0

Oliver
Oliver

Reputation: 45119

As stated in dozens of questions before there are two candidates for easy reading of .csv files:

Upvotes: 0

maephisto
maephisto

Reputation: 5182

I'd go like this : load the csv file to a datatable and then handle it however i want, by column, or by row. here's a link with some directions

Upvotes: 0

Oded
Oded

Reputation: 499352

You can use the TextFieldParser class that is in the Microsoft.VisualBasic.FileIO namespace.

It will parse the file and the resulting object can be queried, so you will be able to get the values in the first column.

Upvotes: 1

npinti
npinti

Reputation: 52195

You can read line by line and use the split method to split the line you have read into colums and keep the column you want. Here is a simple example on how to use the split method.

Upvotes: 0

Peyton Crow
Peyton Crow

Reputation: 882

Try using this A Fast CSV Reader

Upvotes: 5

Related Questions