如何使用C#和FileHelpers 3.1中的智能格式检测器动态解析CSV文件? [英] How can one to dynamically parse a CSV file using C# and the Smart Format Detector in FileHelpers 3.1?

查看:451
本文介绍了如何使用C#和FileHelpers 3.1中的智能格式检测器动态解析CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此 FileHelpers 3.1示例,您可以自动检测CSV文件格式使用FileHelpers.Detection.SmartFormatDetector类。

As per in this FileHelpers 3.1 example, you can automatically detect a CSV file format using the FileHelpers.Detection.SmartFormatDetector class.

但示例不再进一步。如何使用此信息动态解析CSV文件?它必须与DelimitedFileEngine有关,但我看不到如何。

But the example goes no further. How do you use this information to dynamically parse a CSV file? It must have something to do with the DelimitedFileEngine but I cannot see how.

更新:

我想出了一个可能的方法,但不得不求助于使用反射(这感觉不对)。有没有另一个/更好的方法?也许使用System.Dynamic?无论如何,这里是我到目前为止的代码,它不漂亮,但它的工作原理:

I figured out a possible way but had to resort to using reflection (which does not feel right). Is there another/better way? Maybe using System.Dynamic? Anyway, here is the code I have so far, it ain't pretty but it works:

        // follows on from smart detector example

        FileHelpers.Detection.RecordFormatInfo lDetectedFormat = formats[0];

        Type lDetectedClass = lDetectedFormat.ClassBuilderAsDelimited.CreateRecordClass();

        List<FieldInfo> lFieldInfoList = new List<FieldInfo>(lDetectedFormat.ClassBuilderAsDelimited.FieldCount);
        foreach (FileHelpers.Dynamic.DelimitedFieldBuilder lField in lDetectedFormat.ClassBuilderAsDelimited.Fields)
            lFieldInfoList.Add(lDetectedClass.GetField(lField.FieldName));

        FileHelperAsyncEngine lFileEngine = new FileHelperAsyncEngine(lDetectedClass);
        int lRecNo = 0;
        lFileEngine.BeginReadFile(cReadingsFile);
        try
        {
            while (true)
            {
                object lRec = lFileEngine.ReadNext();
                if (lRec == null)
                    break;

                Trace.WriteLine("Record " + lRecNo);
                lFieldInfoList.ForEach(f => Trace.WriteLine("   " + f.Name + " = " + f.GetValue(lRec)));

                lRecNo++;
            }
        }
        finally
        {
            lFileEngine.Close();
        }


推荐答案

当我使用SmartFormatDetector确定传入的Delimited文件的确切格式,您可以在以下应用程序中使用:

As I use the SmartFormatDetector to determine the exact format of the incoming Delimited files you can use following appoach:

    private DelimitedClassBuilder GetFormat(string file)
    {
        var detector = new FileHelpers.Detection.SmartFormatDetector();
        var format = detector.DetectFileFormat(file);

        return format.First().ClassBuilderAsDelimited;
    }

    private List<T> ConvertFile2Objects<T>(string file, out DelimitedFileEngine engine)
    {
        var format = GetSeperator(file); // Get Here your FormatInfo
        engine = new DelimitedFileEngine(typeof(T)); //define your DelimitdFileEngine

        //set some Properties of the engine with what you need
        engine.ErrorMode = ErrorMode.SaveAndContinue; //optional
        engine.Options.Delimiter = format.Delimiter;
        engine.Options.IgnoreFirstLines = format.IgnoreFirstLines;
        engine.Options.IgnoreLastLines = format.IgnoreLastLines;
        //process
        var ret = engine.ReadFileAsList(file);
        this.errorCount = engine.ErrorManager.ErrorCount;
        var err = engine.ErrorManager.Errors;
        engine.ErrorManager.SaveErrors("errors.out");
        //return records do here what you need
        return ret.Cast<T>().ToList();
    }

这是我在项目中使用的一种方法,必须处理多种类型的分隔文件。

This is an approach I use in a project, where I only know that I have to process Delimited files of multiple types.

注意:
我注意到,我收到的文件SmartFormatDetector有一个问题与制表符分隔符。也许这应该考虑。

Attention: I noticed that with the files I recieved the SmartFormatDetector has a problem with tab delimiter. Maybe this should be considered.

免责声明:此代码不完善,但处于可用状态。建议修改和/或重构。

Disclaimer: This code is not perfected but in a usable state. Modification and/or refactoring is adviced.

这篇关于如何使用C#和FileHelpers 3.1中的智能格式检测器动态解析CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆