开发者

Handling quotes with FileHelpers

开发者 https://www.devze.com 2023-02-12 07:46 出处:网络
I\'m using the excellent FileHelpers library to import many csv files, but run into a problem. I have a csv file with these three example lines

I'm using the excellent FileHelpers library to import many csv files, but run into a problem. I have a csv file with these three example lines

id,text,number
120,"good line t开发者_运维百科his one",789
121,""not good" line", 4456
122,,5446

and this (example) class

  [IgnoreFirst(1)]
  [IgnoreEmptyLines()]
  [DelimitedRecord(",")]
  public sealed class JOURNAL
  {
    public Int32 ID;

    [FieldQuoted('"', QuoteMode.AlwaysQuoted, MultilineMode.NotAllow)]
    public string TEXT;

    public Int32? NUMBER;
  }

The problem with QuoteMode.AlwaysQuoted is that ID 122 will fail with error:

The field 'TEXT' not begin with the QuotedChar at line 3. You can use FieldQuoted(QuoteMode.OptionalForRead) to allow optional quoted field

Switching to QuoteMode.OptionalForRead will fail with an error for id 121:

The field TEXT is quoted but the quoted char: " not is just before the separator (You can use [FieldTrim] to avoid this error)

So how can I handle a csv that has empty fields with no quotes AND quoted text fields with extra quotes in the text?


That looks like a case that we don't support, let me add a test case and make it work in both modes, for the first one we need to validate if semantic is correct, ie. if QuoteMode.AlwaysQuoted can allow ,, or must be ,"", but the second option must work :) Cheers

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号