Multiple CSV structures with FileHelpers

情到浓时终转凉″ 提交于 2020-01-13 05:48:53

问题


I am making a site that consumes a csv file this file can come in 2 formats(maybe more in the future).

Structure 1

Header 1 Header 2 Header 3 Header 4 
a          b      c       d
x          x      x       x

Structure 2

Header 1 Header 4
a          d
x          x

the above is how it would be shown in excel(if looking at raw it would be all comma separated)

The reason why I want to have 2 structures is because I am using trying to leverage a 3rd party site that user can export their data from. This site exports it as a csv file with the first row being the headers. I really only care about 2 of the headers and the reset is not needed at this current time (but you have to export all columns can't pick and choose).

The second structure is if the user does not wish to use this site because they don't want to, uncomfortable to do so and etc. They have an option of opening excel up and writing the data manually in and then saving it as a csv file.

So for manual people I want to make it as simple as possible as if I am not using Header 2 and Header 4 data why should I bother getting them to enter it in? However at the same time if people go through the first way and export the data I don't want them to have to go and load the file into excel up and remove 2 columns.

I am going to require the header must always be intact and be the first row. The only idea I came up with is read the first row and see the order of the headers. If it has 4 headers in the exact order then render it one way. If only 2 headers in that order render it another way.

I know that FileHelpers has the ability to do multiple delimiters and choose how to render it but since I am looking at headers I am not sure if this baked in or if I need to somehow write it myself and then tell it what to do.

Does anyone have an idea if I can do this with filehelpers?

Edit this is what I have so far

MultiRecordEngine engine = new MultiRecordEngine(typeof(Format2), typeof(Format1));
    engine.RecordSelector = new RecordTypeSelector(CustomSelector);

    using (TextReader textReader = new StreamReader(stream))
    {
        if (engine.RecordType == typeof(Format2))
        {
            var myArry = engine.ReadStream(textReader) as Format2[];

        }
        else if(engine.RecordType == typeof(Format1))
        {
            var myArry = engine.ReadStream(textReader) as Format1[];
        }



    }

回答1:


Here are a couple of suggested approaches:

Read the first line of the file (outside of FileHelpers) and determine which format is being used before creating the engine

if (firstLineOfFile.Contains("Header 2"))
    FileHelperEngine engine = new FileHelperEngine(typeof(Format1)); 
else
    FileHelperEngine engine = new FileHelperEngine(typeof(Format2)); 

Alternatively, you can use the MultiRecordEngine

MultiRecordEngine engine;  
engine = new MultiRecordEngine(typeof(Format1), typeof(Format2)); 
engine.RecordSelector = new RecordTypeSelector(CustomSelector); 

with a selector method something like

Type CustomSelector(MultiRecordEngine engine, string record) 
{ 
    // count the separators to determine which format to return
    int separatorCount = record.Count(f => f == ',');
    if (separatorCount == 4) 
        return typeof(Format1); 
    else 
        return typeof(Format2); 
} 

(The MultiRecordEngine can handle more formats in the future - it has a constructor with a params parameter)




回答2:


With FileHelpers you will need to know the format ahead of time.

Which means that you will have to detect (using the method you described) which format the file is in prior to using FileHelpers to parse it.



来源:https://stackoverflow.com/questions/8797079/multiple-csv-structures-with-filehelpers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!