本文介绍了使用Filehelpers处理DelimitedRecord中的NEWLINE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Am使用出色的FileHelpers库来解析许多不同的文件.其中一个文件具有(某些)行,如下所示:

Am using the excellent FileHelpers library to parse a number of different files. One of these files has (some) lines that look like this

id|name|comments|date
01|edov|bla bla bla bla|2012-01-01
02|john|bla bla bla bla|2012-01-02
03|Pete|bla bla <NEWLINE>
bla bla|2012-03-01
04|Mary|bla bla bla bla|2012-01-01

请注意,ID为3的行在文本中包含换行符.另请注意,注释未用引号引起来,因此[FieldQuoted('"', MultilineMode.AllowForRead)]不会拯救我.

Note that line with id 3 has a newline in the text. Also note the comments are not surrounded by quotes so [FieldQuoted('"', MultilineMode.AllowForRead)] isn't going to save me.

Filehelpers在第4行引发异常:

Filehelpers throws an exception on line 4:

反正我可以使用FileHelpers解析此文件吗?

Is there anyway I can parse this file with FileHelpers?

推荐答案

在将输入传递给FileHelpers引擎之前,您必须通过在第三个字段添加引号来更正输入.如以下程序所示,使用LINQ很容易.

You have to correct the input by adding quotes to the third field before passing it to the FileHelpers engine. It's easy to do with LINQ as demonstrated in the following program.

[DelimitedRecord("|")]
[IgnoreFirst(1)]
public class ImportRecord
{
    public string Id;
    public string Name;
    [FieldQuoted(QuoteMode.AlwaysQuoted, MultilineMode.AllowForRead)]
    public string Comments;
    public string Date;
}

class Program
{
    static void Main(string[] args)
    {
        var engine = new FileHelperEngine<ImportRecord>();

        string input = "id|name|comments|date" + Environment.NewLine +
                              "01|edov|bla bla bla bla|2012-01-01" + Environment.NewLine +
                              "02|john|bla bla bla bla|2012-01-02" + Environment.NewLine +
                              "03|Pete|bla bla" + Environment.NewLine +
                              "bla bla|2012-03-01" + Environment.NewLine +
                              "04|Mary|bla bla bla bla|2012-01-01";

        // Modify import to add quotes to multiline fields
        var inputSplitAtSeparator = input.Split('|');
        // Add quotes around the field after every third '|'
        var inputWithQuotesAroundCommentsField = inputSplitAtSeparator.Select((x, i) => (i % 3 == 2) ? "\"" + x + "\"" : x);
        var inputJoinedBackTogether = String.Join("|", inputWithQuotesAroundCommentsField);

        ImportRecord[] validRecords = engine.ReadString(inputJoinedBackTogether);

        // Check the third record
        Assert.AreEqual("03", validRecords[2].Id);
        Assert.AreEqual("Pete", validRecords[2].Name);
        Assert.AreEqual("bla bla" + Environment.NewLine + "bla bla", validRecords[2].Comments);
        Assert.AreEqual("2012-03-01", validRecords[2].Date);

        Console.WriteLine("All assertions passed");
        Console.ReadKey();
    }
}

这篇关于使用Filehelpers处理DelimitedRecord中的NEWLINE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 21:04