Read a specific column from a csv file in C#

后端 未结 6 1386
天命终不由人
天命终不由人 2021-01-23 21:44

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 mo

相关标签:
6条回答
  • 2021-01-23 22:19

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

    • FileHelpers
    • Fast CSV Reader
    0 讨论(0)
  • 2021-01-23 22:24

    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.

    0 讨论(0)
  • 2021-01-23 22:24

    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

    0 讨论(0)
  • 2021-01-23 22:28

    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.

    0 讨论(0)
  • 2021-01-23 22:31

    Try using this A Fast CSV Reader

    0 讨论(0)
  • 2021-01-23 22:38

    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>();
    
    0 讨论(0)
提交回复
热议问题