Parsing CSV File enclosed with quotes in C#

后端 未结 10 1961
一整个雨季
一整个雨季 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:09

    As no (decent) .csv parser can parse non-csv-data correctly, the task isn't to parse the data, but to fix the file(s) (and then to parse the correct data).

    To fix the data you need a list of bad rows (to be sent to the person responsible for the garbage for manual editing). To get such a list, you can

    1. use Access with a correct import specification to import the file. You'll get a list of import failures.

    2. write a script/program that opens the file via the OLEDB text driver.

    Sample file:

    "Id","Remark","DateDue"
    1,"This is good",20110413
    2,"This is ""good""",20110414
    3,"This is ""good"","bad",and "ugly",,20110415
    4,"This is ""good""" again,20110415
    

    Sample SQL/Result:

     SELECT * FROM [badcsv01.csv]
     Id Remark               DateDue   
      1 This is good         4/13/2011 
      2 This is "good"       4/14/2011 
      3 This is "good",        NULL    
      4 This is "good" again 4/15/2011 
    
    SELECT * FROM [badcsv01.csv] WHERE DateDue Is Null
     Id Remark          DateDue 
      3 This is "good",  NULL   
    

提交回复
热议问题