Reading CSV file and storing values into an array

后端 未结 19 1439
猫巷女王i
猫巷女王i 2020-11-22 06:35

I am trying to read a *.csv-file.

The *.csv-file consist of two columns separated by semicolon (\";\").

I am able

19条回答
  •  抹茶落季
    2020-11-22 07:14

    My favourite CSV parser is one built into .NET library. This is a hidden treasure inside Microsoft.VisualBasic namespace. Below is a sample code:

    using Microsoft.VisualBasic.FileIO;
    
    var path = @"C:\Person.csv"; // Habeeb, "Dubai Media City, Dubai"
    using (TextFieldParser csvParser = new TextFieldParser(path))
    {
     csvParser.CommentTokens = new string[] { "#" };
     csvParser.SetDelimiters(new string[] { "," });
     csvParser.HasFieldsEnclosedInQuotes = true;
    
     // Skip the row with the column names
     csvParser.ReadLine();
    
     while (!csvParser.EndOfData)
     {
      // Read current line fields, pointer moves to the next line.
      string[] fields = csvParser.ReadFields();
      string Name = fields[0];
      string Address = fields[1];
     }
    }
    

    Remember to add reference to Microsoft.VisualBasic

    More details about the parser is given here: http://codeskaters.blogspot.ae/2015/11/c-easiest-csv-parser-built-in-net.html

提交回复
热议问题