【发布时间】: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”?
【问题讨论】:
-
哇,那一行真的很有帮助,非常感谢!