简体   繁体   English

FileHelpers 在字段中引用和逗号

[英]FileHelpers quote and comma in fields

I have a csv file that I am parsing with FileHelpers and I have a situation where both a quote and a comma can appear in a field:我有一个使用 FileHelpers 解析的 csv 文件,并且我遇到了一个字段中同时出现引号和逗号的情况:

Comma:逗号:

323,"PC","28/02/2014","UNI001","5000",0,"Return","Returned Goods, damaged",88.00,15.40,"T1","N",0.00,"R","-",

Quote引用

 148,"SI","13/01/2014","CGS001","4000",1,"5","17" Monitor",266.00,45.39,"T1","Y",311.39,"R","-", 

My class is:我的课是:

[DelimitedRecord(",")]
public class Transaction
{
    public int TRAN_NUMBER;
    [FieldQuoted('"', QuoteMode.OptionalForBoth)]
    public string TypeText;
    [FieldQuoted('"', QuoteMode.OptionalForBoth)]
    public string DATE;
    [FieldQuoted('"', QuoteMode.OptionalForBoth)]
    public string TransactionAccount;
    [FieldQuoted('"', QuoteMode.OptionalForBoth)]
    public string NOMINAL_CODE;

    public int DEPT_NUMBER;

    [FieldQuoted('"', QuoteMode.OptionalForBoth)]
    public string INV_REF;

    [FieldQuoted('"', QuoteMode.OptionalForBoth)]
    public string DETAILS;

    public string NET_AMOUNT;
    public string TAX_AMOUNT;
    [FieldQuoted('"', QuoteMode.OptionalForBoth)]
    public string TaxCodeName;
    [FieldQuoted('"', QuoteMode.OptionalForBoth)]
    public string PAID_FLAG;

    public string AMOUNT_PAID;

    [FieldQuoted('"', QuoteMode.OptionalForBoth)]
    public string VatReconText;
    [FieldQuoted('"', QuoteMode.OptionalForBoth)]
    public string BankReconText;

    public string RECON_DATE;
}

I have found this thread FileHelpers nested quotes and commas - parsing error我发现这个线程FileHelpers nested quotes and commas - parsing error

engine.BeforeReadRecord += (sender, args) => 
args.RecordLine = args.RecordLine.Replace(@"""", "'");

But it only helps with quotes appearing problem and not the commas.但它只有助于引号出现问题,而不是逗号。

Can both of these problem be solved with FileHelpers or I should look for an alternative solution?这两个问题都可以用 FileHelpers 解决还是我应该寻找替代解决方案?

You can implement a BeforeReadRecord event to 'fix' your bad lines.你可以实现一个BeforeReadRecord事件来“修复”你的坏行。

FileHelperEngine engine = new FileHelperEngine<Transaction>(); 
engine.BeforeReadRecord += BeforeEvent; 

private void BeforeEvent(EngineBase engine, BeforeReadRecordEventArgs e)
{
    var line = e.RecordLine;

    // you have to write the following replacement routine...
    var fixedLine = ReplaceEmbeddedCommasAndQuotesWithSomethingDifferent(line); 

    e.RecordLine = fixedLine; // replace the line with the fixed version
}

And after you've read the records in you could process them to reverse the replacement process to fix them.在您阅读了其中的记录后,您可以处理它们以逆转替换过程来修复它们。

If you prefer to define all the logic in the FileHelpers class itself, you can implement INotifyRead<Transaction> instead of using the event.如果您更喜欢在 FileHelpers 类本身中定义所有逻辑,则可以实现INotifyRead<Transaction>而不是使用事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM