使用CsvHelper将所有值从CSV读入列表 [英] Read all values from CSV into a List using CsvHelper
问题描述
所以我一直在读,我不应该写我自己的CSV读写器,所以我一直在试图使用通过nuget安装的CsvHelper库。 CSV文件是灰度图像,行数为图像高度,数字列为宽度。我想把这些值逐行读到单个 List< string>
或 List< byte>
/ p>
我到目前为止的代码是:
使用CsvHelper;
public static List< string> ReadInCSV(string absolutePath)
{
IEnumerable< string> allValues;
使用(TextReader fileReader = File.OpenText(absolutePath))
{
var csv = new CsvReader(fileReader);
csv.Configuration.HasHeaderRecord = false;
allValues = csv.GetRecords< string>
}
return allValues.ToList< string>();
}
但 allValues.ToList< string> / code> is throwing a:
CsvConfigurationException未由用户代码处理
CsvHelper.dll中发生类型为CsvHelper.Configuration.CsvConfigurationException的异常,但未在用户代码中处理
>附加信息:继承IEnumerable的类型不能自动映射。你不小心调用了GetRecord或WriteRecord,而不是调用GetRecords或WriteRecords对记录列表进行操作。
GetRecords
可能期待我自己的自定义类,但我只是想要的值作为一些原始类型或字符串。此外,我怀疑整个行被转换为单个字符串,而不是每个值都是单独的字符串。
to @Marc L's post you can try this:
public static List< string& ReadInCSV(string absolutePath){
List< string> result = new List< string>();
string value;
using(TextReader fileReader = File.OpenText(absolutePath)){
var csv = new CsvReader(fileReader);
csv.Configuration.HasHeaderRecord = false;
while(csv.Read()){
for(int i = 0; csv.TryGetField< string>(i,out value); i ++){
result.Add(value) ;
}
}
}
返回结果;
}
So I've been reading that I shouldn't write my own CSV reader/writer, so I've been trying to use the CsvHelper library installed via nuget. The CSV file is a grey scale image, with the number of rows being the image height and the number columns the width. I would like to read the values row-wise into a single List<string>
or List<byte>
.
The code I have so far is:
using CsvHelper;
public static List<string> ReadInCSV(string absolutePath)
{
IEnumerable<string> allValues;
using (TextReader fileReader = File.OpenText(absolutePath))
{
var csv = new CsvReader(fileReader);
csv.Configuration.HasHeaderRecord = false;
allValues = csv.GetRecords<string>
}
return allValues.ToList<string>();
}
But allValues.ToList<string>()
is throwing a:
CsvConfigurationException was unhandled by user code
An exception of type 'CsvHelper.Configuration.CsvConfigurationException' occurred in CsvHelper.dll but was not handled in user code
Additional information: Types that inherit IEnumerable cannot be auto mapped. Did you accidentally call GetRecord or WriteRecord which acts on a single record instead of calling GetRecords or WriteRecords which acts on a list of records?
GetRecords
is probably expecting my own custom class, but I'm just wanting the values as some primitive type or string. Also, I suspect the entire row is being converted to a single string, instead of each value being a separate string.
According to @Marc L's post you can try this:
public static List<string> ReadInCSV(string absolutePath) {
List<string> result = new List<string>();
string value;
using (TextReader fileReader = File.OpenText(absolutePath)) {
var csv = new CsvReader(fileReader);
csv.Configuration.HasHeaderRecord = false;
while (csv.Read()) {
for(int i=0; csv.TryGetField<string>(i, out value); i++) {
result.Add(value);
}
}
}
return result;
}
这篇关于使用CsvHelper将所有值从CSV读入列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!