使用CSVhelper C#合并具有不同标题的CSV文件 [英] Merging CSV files with different headers using CSVhelper C#

查看:311
本文介绍了使用CSVhelper C#合并具有不同标题的CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试使用CSVhelper将目录中的多个.csv文件合并为一个.csv文件时.在目录中,有50个.csv文件,在这50个文件中,有两组文件结构,一组具有7列,一组具有6.每个文件具有完全相同的前5个标头,但是取决于文件,后两列将改变.

When trying to merge multiple .csv files from a directory into one .csv file using CSVhelper. In the directory there are 50 .csv files, among these 50 files there are two sets of file structures, one with 7 columns and one with 6. Every file has the exact same first 5 headers however depending on the file the last two columns will change.

CSV文件格式1的示例:

Example of CSVfile format 1:

CSV文件格式2的示例:

Example of CSVfile format 2:

目录中的每个文件都将在列中保存具有不同数据的这些结构之一.新文件的输出将包含来自操作",代码"和错误消息"所有列的数据.如果我仅将文件与示例1的结构一起使用,则文件会完美组合在一起.但是,如果我同时包含两种结构的文件,并尝试在新文件中使用示例2中的"ErrorIPAddress",则会出现以下错误:

Every file in the directory will hold either of these structures with different data in the columns. The output of the new file will have data from all the columns bar Action, Code and Error Message. If i use files only with the structure of example 1 the file comes together perfectly. However, if i include files with both structures and try and use 'ErrorIPAddress' from example 2 in my new file i get the following error:

在CsvHelper.dll中发生了类型为'CsvHelper.TypeConversion.CsvTypeConverterException'的未处理异常

An unhandled exception of type 'CsvHelper.TypeConversion.CsvTypeConverterException' occurred in CsvHelper.dll

在这一行:`IEnumerable dataRecord = reader.GetRecords().ToList();

On this line: `IEnumerable dataRecord = reader.GetRecords().ToList();

我的问题是:如何使用一个文件中的列而不是另一个文件中的列?我尝试使用以下命令映射它:Map(m => m.ErrorIPAddress).Index(5);,并且我相信这是导致我出现问题的行,好像我将其注释掉了,该错误不会持续存在,但是显然我不会将所需的数据获取到新数据中. csv.如果我尝试使用以下名称进行映射:Map( m => m.ErrorIPAddress ).Name( "ErrorIPAddress" );,我将收到错误消息,指出ErrorIPAddress不在.csv文件中,因为并非所有文件都具有该列,所以它不会.

My question is: How to use columns from one file thats not in the other? I have tried mapping it with the following:Map(m => m.ErrorIPAddress).Index(5); and i believe this is the line causing me the issue as if i comment it out the error doesn't persist however obviously i won't get the data i need into the new .csv. If i try and map by name with: Map( m => m.ErrorIPAddress ).Name( "ErrorIPAddress" ); I get the error message that ErrorIPAddress is not in the .csv file which it won't be as not all files have that column.

.csv输出格式:

Output .csv format:

ErrorIPAddress列将以格式2生成最后一列.

The final column will be generated by the ErrorIPAddress column in format 2.

推荐答案

我假设您对所有看起来像这样的字段使用单个类定义:

I'm assuming you are using a single class definition with all the fields that looks something like this:

public class StudentWebAccess
{
    public int StudentID { get; set; }
    public string Gender { get; set; }
    public int Grade { get; set; }        
    public int IPAddress { get; set; } // Also ErrorIPAddress?
    public DateTime DateTime { get; set; }
    public string Action { get; set; }
    public string Code { get; set; } // Also ErrorMessage?
}

因此,为了读取文件格式2,您正在使用CsvClassMap,但未正确匹配属性和字段名称.它应该看起来像这样:

So in order to read file format 2 you are using CsvClassMap but aren't matching the properties and field names correctly. It should look something like this:

public class CsvFile2Map : CsvClassMap<StudentWebAccess>
{
    public CsvFile2Map()
    {            
        Map(m => m.IPAddress).Name("ErrorIPAddress");
        Map(m => m.Code).Name("ErrorMessage");
    }
}

如果您的类文件使用ErrorIPAddress而不是IPAddress,则必须撤消映射.

If your class file uses ErrorIPAddress instead of IPAddress you have to reverse the mapping.

Map(m => m.ErrorIPAddress).Name("IPAddress");

这篇关于使用CSVhelper C#合并具有不同标题的CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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