gpt4 book ai didi

c# - FileHelpers 字段中的引号和逗号

转载 作者:行者123 更新时间:2023-11-30 12:40:01 27 4
gpt4 key购买 nike

我有一个 csv 文件,我正在使用 FileHelpers 进行解析,并且我遇到了引号和逗号都可以出现在字段中的情况:

逗号:

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

报价

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

我的类(class)是:

[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;
}

我找到了这个主题 FileHelpers nested quotes and commas - parsing error

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

但它只有助于解决引号出现的问题,而不是逗号。

这两个问题都可以用 FileHelpers 解决吗?还是我应该寻找替代解决方案?

最佳答案

您可以实现 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
}

在您读取了其中的记录后,您可以处理它们以逆向替换过程来修复它们。

如果您更喜欢在 FileHelpers 类本身中定义所有逻辑,您可以实现 INotifyRead<Transaction>而不是使用事件。

关于c# - FileHelpers 字段中的引号和逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42974657/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com