英文:
CsvHelper working differently with TextDelimiter & FieldDelimiter
问题
我们正在使用CsvHelper来处理csv和dat文件。对于dat文件的处理,我们保留了"TextDelimiter": "þ"和"FieldDelimiter": "¶"。
如果dat文件中没有双引号("),那么csvhelper正常工作。
如果dat文件中有双引号("),那么csvhelper会错误地拆分数据。
using (var reader = new StreamReader(datFilePath))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
if (LoadFileExtn.ToLower() == ".dat")
{
//csv.Configuration.IgnoreQuotes = true;
csv.Configuration.Delimiter = "¶";
csv.Configuration.Quote = "þ";
}
else
{
csv.Configuration.Delimiter = "|";
}
csv.Read();
csv.ReadHeader();
List<IDictionary<string, object>> dataRecords = csv.GetRecords<dynamic>()
.Select(x => (IDictionary<string, object>)x)
.ToList();
foreach (var record in dataRecords)
{
}
}
LoadFile:
þInternal_File_IdþþIdþþFileNameþþEMAIL_FROMþþSubjectþþEMAIL_RECEIVED_DATE_TIMEþ
þ248073þþGRM00001504þþSCS CRUDE STRADDLES 11-19.msgþþAAA <aaa@mail.com>þþTest Mail 1þþ2001-04-17 04:13:00þ
þ248074þþGRM00001505þþPlease provide your NT Login Id and "_pc" Id RE: Vol Smil Authorization.msgþþAAA <aaa@mail.com>þþPlease provide your NT Login Id and "_pc" Id RE: Vol Smil Authorizationþþ2001-04-17 04:13:00þ
英文:
We are using CsvHelper for processing csv & dat files. For the dat files processing, we are keeping the "TextDelimiter": "þ" & "FieldDelimiter": "¶"
If the dat file is NOT having any double quotes ('"'), then the csvhelper is working fine.
If the dat file is having a double quotes ('"'), then the csvhelper is splitting the data incorrectly.
using (var reader = new StreamReader(datFilePath))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
if (LoadFileExtn.ToLower() == ".dat")
{
//csv.Configuration.IgnoreQuotes = true;
csv.Configuration.Delimiter = "¶";
csv.Configuration.Quote = "þ";
}
else
{
csv.Configuration.Delimiter = "|";
}
csv.Read();
csv.ReadHeader();
List<IDictionary<string, object>> dataRecords = csv.GetRecords<dynamic>()
.Select(x => (IDictionary<string, object>)x)
.ToList();
foreach (var record in dataRecords)
{
}
}
LoadFile:
þInternal_File_IdþþIdþþFileNameþþEMAIL_FROMþþSubjectþþEMAIL_RECEIVED_DATE_TIMEþ
þ248073þþGRM00001504þþSCS CRUDE STRADDLES 11-19.msgþþAAA <aaa@mail.com>þþTest Mail 1þþ2001-04-17 04:13:00þ
þ248074þþGRM00001505þþPlease provide your NT Login Id and "_pc" Id RE: Vol Smil Authorization.msgþþAAA <aaa@mail.com>þþPlease provide your NT Login Id and "_pc" Id RE: Vol Smil Authorizationþþ2001-04-17 04:13:00þ
答案1
得分: 1
我相信这是因为双引号也默认作为转义字符。我将转义字符更改为与您的引号字符相同,看起来对我来说正常工作。
此外,看起来您正在使用较旧版本的 CsvHelper
。我提供的示例是针对更新版本的,但您应该能够在您的代码中添加 csv.Configuration.Escape = 'þ';
。
var config = new CsvConfiguration(CultureInfo.InvariantCulture){
Delimiter = '¶',
Quote = 'þ',
Escape = 'þ'
};
using (var reader = new StreamReader(@"C:\Users\dspecht\Downloads\SampleDatFile_20230417_2.dat"))
using (var csv = new CsvReader(reader, config))
{
csv.Read();
csv.ReadHeader();
List<IDictionary<string, object>> dataRecords = csv.GetRecords<dynamic>()
.Select(x => (IDictionary<string, object>)x)
.ToList();
foreach (var record in dataRecords)
{
Console.WriteLine(record);
}
}
英文:
I believe it is because the double quote also defaults as the escape character. I changed the escape character to be the same as your quote character and it appears to be working correctly for me.
Also it looks like you are using an older version of CsvHelper
. The example I'm providing is for the newer version, but you should be able to add csv.Configuration.Escape = 'þ';
to your code.
var config = new CsvConfiguration(CultureInfo.InvariantCulture){
Delimiter = "¶",
Quote = 'þ',
Escape = 'þ'
};
using (var reader = new StreamReader(@"C:\Users\dspecht\Downloads\SampleDatFile_20230417_2.dat"))
using (var csv = new CsvReader(reader, config))
{
csv.Read();
csv.ReadHeader();
List<IDictionary<string, object>> dataRecords = csv.GetRecords<dynamic>()
.Select(x => (IDictionary<string, object>)x)
.ToList();
foreach (var record in dataRecords)
{
Console.WriteLine(record);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论