How to split csv whose columns may contain ,

前端 未结 8 1263
我寻月下人不归
我寻月下人不归 2020-11-22 06:35

Given

2,1016,7/31/2008 14:22,Geoff Dalgas,6/5/2011 22:21,http://stackoverflow.com,"Corvallis, OR",7679,351,81,b437f461b3fd27387c5d8ab47a293d35,34

8条回答
  •  耶瑟儿~
    2020-11-22 07:20

    With Cinchoo ETL - an open source library, it can automatically handles columns values containing separators.

    string csv = @"2,1016,7/31/2008 14:22,Geoff Dalgas,6/5/2011 22:21,http://stackoverflow.com,""Corvallis, OR"",7679,351,81,b437f461b3fd27387c5d8ab47a293d35,34";
    
    using (var p = ChoCSVReader.LoadText(csv)
        )
    {
        Console.WriteLine(p.Dump());
    }
    

    Output:

    Key: Column1 [Type: String]
    Value: 2
    Key: Column2 [Type: String]
    Value: 1016
    Key: Column3 [Type: String]
    Value: 7/31/2008 14:22
    Key: Column4 [Type: String]
    Value: Geoff Dalgas
    Key: Column5 [Type: String]
    Value: 6/5/2011 22:21
    Key: Column6 [Type: String]
    Value: http://stackoverflow.com
    Key: Column7 [Type: String]
    Value: Corvallis, OR
    Key: Column8 [Type: String]
    Value: 7679
    Key: Column9 [Type: String]
    Value: 351
    Key: Column10 [Type: String]
    Value: 81
    Key: Column11 [Type: String]
    Value: b437f461b3fd27387c5d8ab47a293d35
    Key: Column12 [Type: String]
    Value: 34
    

    For more information, please visit codeproject article.

    Hope it helps.

提交回复
热议问题