【问题标题】:CsvFile Header Row does not match output, ex(Header = Tol +, Console output = Tol_plus) C#,csvHelperCsvFile Header Row 与输出不匹配,ex(Header = Tol +, Console output = Tol_plus) C#,csvHelper
【发布时间】:2020-06-10 01:50:22
【问题描述】:

您好,我正在使用 csvHelper 解析 csvFile,我遇到了障碍。标头中的标头之一是“Tol +”。我无法将其作为变量名,因此我使用自动映射类将其映射为“Tol_plus”。但是,当我输出文件时,我收到:

(Console output of Header Row below)
Measurement,Nominal,Tol_plus

我想要什么:

(How Header Row  actually looks below)
Measurement,Nominal,Tol +

这是我目前的代码:

public class Foo
        {

            public string Measurement { get; set; }//read as string
            public string Nominal { get; set; } //reads in data from here
            public string Tol_plus { get; set; }//reads in data from here 
        }

public sealed class FooMap : ClassMap<Foo>
        {
            public FooMap()
            {
                Map(m => m.Measurement).Name("Measurement");//maps the variables from the class above to things in quotes
                Map(m => m.Nominal).Name("Nominal");

                Map(m => m.Tol_plus).Name("Tol +");
            }
        }
//main method
void main()
 var textwriter = Console.Out;
             using (var csvWriter = new CsvWriter(textwriter, CultureInfo.InvariantCulture))
             using (var reader = new StreamReader(@"path.csv"))
             using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
             {
                csv.Read();
                csv.ReadHeader();
                csv.Configuration.RegisterClassMap<FooMap>();//necessary for mapping 
                var records = csv.GetRecords<Foo>();
                csvWriter.WriteRecords(records);
              }
//The output here gives me: 
Measurement,Nominal,Tol_plus

为此,我尝试将字符串文字传递给 Tol_plus,但没有奏效,是否有办法映射它以打印出“Tol +”而不是“Tol_plus”?

【问题讨论】:

标签: c# csv parsing csvhelper


【解决方案1】:

根据 Edney 的评论:

public class Foo
        {

            public string Measurement { get; set; }//read as string
            public string Nominal { get; set; } //reads in data from here

            [CsvHelper.Configuration.Attributes.Name("Tol +")]
            public string Tol_plus { get; set; }//reads in data from here 
        }

通过添加行:

[CsvHelper.Configuration.Attributes.Name("Tol +")]

它在输出过程中将名称更改为“Tol +”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多