FileHelpers和CSV:当记录可以无限制地水平扩展时该做什么 [英] FileHelpers and CSV: what to do when a record can expand unbounded, horizontally
问题描述
我尝试使用FileHelpers解析此类型的CSV文件:
I'm trying to parse this type of CSV file with FileHelpers:
Tom,1,2,3,4,5,6,7,8,9,10
Steve,1,2,3
Bob,1,2,3,4,5,6
Cthulhu,1,2,3,4,5
Greg,1,2,3,4,5,6,7,8,9,10,11,12,13,14
我不知道如何解析这与FileHelpers。我想象我应该能够这样做:
I can't figure out how to parse this with FileHelpers. I would imagine I should be able to do something like this:
[DelimitedRecord(",")]
public class MyRecord
{
public string Name;
public List<int> Values;
}
但这似乎不可能与FileHelpers。我最好的做法是这样的:
But that doesn't appear to be possible with FileHelpers. The best I can seem to do is this:
[DelimitedRecord(",")]
public class MyRecord
{
public string Name;
public string Values;
public string[] ActualValuesInNiceArray
{
get { return Values.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries); }
}
}
然后我需要拆分值
在逗号上获取每个记录的值集。如果我必须手动解析每个记录的一部分,使用FileHelpers似乎不太重要。
I then would need to split Values
on commas to get the set of values for each record. Doesn't seem to be much of a point in using FileHelpers if I have to manually parse a portion of each record.
我缺少什么?我已经阅读docs /示例,但似乎找不到我的格式的解决方案。 Excel对我的格式没有麻烦,所以我想象有一个方法来做一个现有的免费库(FileHelpers或一些其他库)。任何想法?
Am I missing something? I've gone over docs/examples, but can't seem to find a solution for my format. Excel has no trouble with my format, so I would imagine there is a way to do it with an existing free library (FileHelpers or some other library). Any ideas?
推荐答案
您可以使用数组字段 / p>
You can use an Array Field and the library will do the work:
[DelimitedRecord(",")]
public class MyRecord
{
public string Name;
public int[] Values;
}
您甚至可以使用[FieldArrayLength(2,8)]
You can even use [FieldArrayLength(2, 8)]
[DelimitedRecord(",")]
public class MyRecord
{
public string Name;
[FieldArrayLength(2, 8)]
public int[] Values;
}
设置最小/最大值的数量
The set the min/max number of values
我强烈建议从这里下载最新版本的库:
I strongly recomend to download the last version of the library from here:
http://teamcity.codebetter.com/viewType.html?buildTypeId=bt65&tab=buildTypeStatusDiv
检查工件部分
这篇关于FileHelpers和CSV:当记录可以无限制地水平扩展时该做什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!