Processing commas in quotation marks when exporting a CSV file to C # 4. All suggestions

Hi everyone, I am reading a CSV file, and I ran into a problem when there is a comma in the field

Everything works until I get a comma in a comma, all the fields will be "around".

string delimiter=","; using (var sr = new StreamReader(fileName)) { sr.ReadLine(); //skip headers while (!sr.EndOfStream) { var readline = sr.ReadLine(); if (readline == null) continue; var fields = readline.Split (delimiter.ToCharArray()); } }if I CANNOT split because of commas within quotation . How can I recode it? 

I cannot use open source libraries or third parties.

Any suggestions?

+2
source share
2 answers

Use the TextFieldParser class.

Description on MSDN:

Provides methods and properties for parsing structured text files.

It lives in the Microsoft.VisualBasic.FileIO namespace, therefore it is not third-party and open source.

This is a managed class, so you can just add a link, import a namespace, and use.

Usage example:

 using(var fileReader = New TextFieldParser("C:\ParserText.txt")) { fileReader.TextFieldType = FieldType.Delimited; fileReader.SetDelimiters(","); ... } 
+3
source

Since third-party libraries are not available, there are several options (maybe there are others):

  • write down a price-oriented CSV reader,
  • use ODBC - see last page here
0
source

Source: https://habr.com/ru/post/1492162/


All Articles