Parsing CSV File enclosed with quotes in C#

后端 未结 10 1973
一整个雨季
一整个雨季 2021-01-21 05:50

I\'ve seen lots of samples in parsing CSV File. but this one is kind of annoying file...

so how do you parse this kind of CSV

\"1\",1/2/2010,\"The sample (\"adas

10条回答
  •  北荒
    北荒 (楼主)
    2021-01-21 06:04

    There is one another open source library, Cinchoo ETL, handle quoted string fine. Here is sample code.

    string csv = @"""1"",1/2/2010,""The sample(""adasdad"") asdada"",""I was pooping in the door ""Stinky"", so I'll be damn"",""AK""";
    
    using (var r = ChoCSVReader.LoadText(csv)
        .QuoteAllFields()
        )
    {
        foreach (var rec in r)
            Console.WriteLine(rec.Dump());
    }
    

    Output:

    [Count: 5]
    Key: Column1 [Type: Int64]
    Value: 1
    Key: Column2 [Type: DateTime]
    Value: 1/2/2010 12:00:00 AM
    Key: Column3 [Type: String]
    Value: The sample(adasdad) asdada
    Key: Column4 [Type: String]
    Value: I was pooping in the door Stinky, so I'll be damn
    Key: Column5 [Type: String]
    Value: AK
    

提交回复
热议问题