忽略引号CsvHelper中的分隔符



我试图在逗号分隔的csv文件中读取,除非字段包含逗号,否则大多数文本都不合格。这方面的一个例子是可以包含逗号的地址行。当这种情况发生时,列在我的映射对象内被分流。

AddressLine1, Email, ID
House Name, test@test.net, 5
"House Name, 20", test@test.net,5

忽略坏结果,这将导致对象

[0] AddressLine1: House Name, Email: test@test.net, ID: 5
[1] AddressLine1: House Name, Email: 20, ID: test@test.net

我已经尝试了各种配置,包括修改模式和转义字符。

var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = true,
Delimiter = ",",                    
HeaderValidated = null,
Mode = CsvMode.Escape,
Escape = '\'
};
谁能告诉我正确的方向?

由于字段"House Name, 20"用引号括起来,所以应该将CsvConfiguration.Mode设置为CsvMode.CsvMode.RFC4180。该enum定义如下:

public enum CsvMode
{
/// Uses RFC 4180 format (default).
/// If a field contains a CsvConfiguration.Delimiter or CsvConfiguration.NewLine,
/// it is wrapped in CsvConfiguration.Quote's.
/// If quoted field contains a CsvConfiguration.Quote, it is preceded by CsvConfiguration.Escape.
RFC4180 = 0,
/// Uses escapes.
/// If a field contains a CsvConfiguration.Delimiter, CsvConfiguration.NewLine,
/// or CsvConfiguration.Escape, it is preceded by CsvConfiguration.Escape.
/// Newline defaults to n.
Escape,
/// <summary>
/// Doesn't use quotes or escapes.
/// This will ignore quoting and escape characters. This means a field cannot contain a
/// CsvConfiguration.Delimiter, CsvConfiguration.Quote, or
/// CsvConfiguration.NewLine, as they cannot be escaped.
NoEscape
}

由于头字段EmailID前面有空格,您可能还想设置TrimOptions = TrimOptions.Trim。因此,您的CsvConfiguration应该看起来像:

var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = true,
// Delimiter = ",", This is the default so no need to change
HeaderValidated = null,
Mode = CsvMode.RFC4180,
TrimOptions = TrimOptions.Trim,
};      

相关:自定义分隔符在CsvHelper中不起作用。

在这里演示小提琴

相关内容

  • 没有找到相关文章

最新更新