Regular [removed]C#) For CSV by RFC 4180

前端 未结 2 1090
鱼传尺愫
鱼传尺愫 2020-12-10 08:21

Requires universal CSV parser by specification RFC 4180. There is the csv file, with all the problems of the specification:

Excel opens the file as it is written in

2条回答
  •  执笔经年
    2020-12-10 08:26

    I would say, forget about regex. CSV can be parsed easily by TextFieldParser class. To do that, you need to be

    using Microsoft.VisualBasic.FileIO;
    

    Then you can use it:

      using (TextFieldParser parser = new TextFieldParser(Stream))
      {
        parser.TextFieldType = FieldType.Delimited;
        parser.SetDelimiters(",");
    
        while (!parser.EndOfData)
        {
          string[] fields = parser.ReadFields();
          foreach (string field in fields)
          {
             // Do your stuff here ...
          }
        }
      }
    

提交回复
热议问题