我正在使用 CsvHelper v26.1.0 读取以下由 分隔的文本文件~:
123~John
234~Joe "Public"
Run Code Online (Sandbox Code Playgroud)
但文件中的双引号导致 CsvHelper 将它们视为错误数据。我通过删除双引号对其进行了测试,效果很好。但问题是,我已经设置了自定义分隔符,为什么双引号仍然导致问题?
public class AccountDtoMap : ClassMap<AccountDto>
{
public AccountDtoMap()
{
Map(m => m.Number).Index(0);
Map(m => m.Name).Index(1);
}
}
var cfg = new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)
{
Delimiter = "~",
HasHeaderRecord = false,
MissingFieldFound = (context) => { errs.Add($"{typeof(T)} missing field: {context.Context.Parser.RawRecord}"); },
BadDataFound = (context) => { errs.Add($"{typeof(T)} bad data: {context.RawRecord}"); },
};
using (var csv = new CsvReader(new StreamReader(file), cfg))
{
csv.Context.RegisterClassMap<AccountDtoMap>();
return csv.GetRecords<T>().ToList();
}
Run Code Online (Sandbox Code Playgroud)
可运行的演示在这里。
要解析问题中显示的 CSV(版本26.1.0),您需要正确配置以下所有CsvConfiguration设置,而不仅仅是分隔符:
Delimiter。用于分隔单个 CSV 行中的字段的字符。(通常,,这里~)。
Escape, 默认"。用于位于需要转义的其他字符之前的字符。
Quote, 默认"。根据RFC4180用于包装需要在开头和结尾加上引号的字段的字符。
上面前三个字符设置的功能在CsvModeenum的注释中进行了解释:
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
}
Run Code Online (Sandbox Code Playgroud)
该字段Joe "Public"包含嵌入的转义字符,这些字符本身并未转义,这导致 CshHelper 报告错误。为了避免该错误,您有多种可能的选择,包括:
设置CsvMode.NoEscape为完全禁用转义和引用:
var cfg = new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)
{
Mode = CsvMode.NoEscape,
// Remainder unchanged.
Run Code Online (Sandbox Code Playgroud)
当然,如果您这样做,您的 CSV 文件不能包含字段中嵌入的分隔符或换行符。
演示小提琴#1在这里。
设置Mode = CsvMode.Escape为禁用引号中的字段换行,并设置为您在实践中不希望在文件中遇到的Escape一些其他字符,例如\或:\t
var cfg = new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)
{
Mode = CsvMode.Escape,
Escape = '\\',
// Remainder unchanged.
Run Code Online (Sandbox Code Playgroud)
即使您这样做,CSV 字段内的分隔符、转义符和换行符仍必须使用选定的转义字符进行正确转义。
演示小提琴#2在这里。
设置Mode = CsvMode.Escape并修复您的文件以正确转义转义字符:
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
}
Run Code Online (Sandbox Code Playgroud)
演示小提琴#3在这里。