解析 .csv 文件时跳过标题行
我使用 StreamReader 对象为 LinqtoCSV 库提供源数据。像这样的…
CsvFileDescription fileDescription = new CsvFileDescription()
{
SeparatorChar = ';',
MaximumNbrExceptions = 20,
};
CsvContext context = new CsvContext();
System.IO.MemoryStream stream = new System.IO.MemoryStream(file.Data);
System.IO.StreamReader reader = new System.IO.StreamReader(stream);
rows = context.Read<T>(reader, fileDescription);
我正在处理标题行中有一个额外分号的 csv 文件,例如
Header1; Header2;Header3;
这会触发错误,因为 CSVtoLinQ 库需要 4 个标题名称而不是 3 个。我可以将选定的行读入 IEnumerable 对象,但我可以不要直接操作 StreamReader 对象的内容。我更愿意继续使用 StreamReader/LinqtoCSV 的组合,因为它提供了很多验证功能,但由于文件头问题,我需要考虑替代方案。
是否有其他 csv 导入库在解析文件时提供(i)基于字段的验证(ii)跳过标题行的方法?
I’m using a StreamReader object to provide source data for the LinqtoCSV library. Something like this…
CsvFileDescription fileDescription = new CsvFileDescription()
{
SeparatorChar = ';',
MaximumNbrExceptions = 20,
};
CsvContext context = new CsvContext();
System.IO.MemoryStream stream = new System.IO.MemoryStream(file.Data);
System.IO.StreamReader reader = new System.IO.StreamReader(stream);
rows = context.Read<T>(reader, fileDescription);
I’m processing csv files which have an extra semi colon in the header row e.g.
Header1; Header2;Header3;
This triggers errors because the CSVtoLinQ library is expecting 4 header names instead of 3. I can read selected rows into an IEnumerable object, but I can’t directly manipulate the contents of the StreamReader object. I’d prefer to continue with the combination of StreamReader/LinqtoCSV as it provides a lot of validation functionality, but because of the file header issue I need to look at alternatives.
Are there other csv import libraries that provide (i) field based validation when parsing a file (ii) a means of skipping the header row?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以编写一个方法来打开文件,检查标头/修复它,然后保存到一个新文件,该文件现在可供现有进程使用。
You could write a method that opens the file, checks the header/fixes it, then saves to a new file which now ready for the existing process.