FileHelpers 和 CSV:当记录可以无限水平扩展时该怎么办
我正在尝试使用 FileHelpers 解析这种类型的 CSV 文件:
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 解析它。我想我应该能够做这样的事情:
[DelimitedRecord(",")]
public class MyRecord
{
public string Name;
public List<int> Values;
}
但这对于 FileHelpers 来说似乎是不可能的。我能做的最好的事情似乎是这样:
[DelimitedRecord(",")]
public class MyRecord
{
public string Name;
public string Values;
public string[] ActualValuesInNiceArray
{
get { return Values.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries); }
}
}
然后我需要用逗号分割 Values
来获取每个记录的值集。如果我必须手动解析每条记录的一部分,那么使用 FileHelpers 似乎没有多大意义。
我错过了什么吗?我已经查看了文档/示例,但似乎无法找到适合我的格式的解决方案。 Excel 对我的格式没有任何问题,所以我想有一种方法可以使用现有的免费库(FileHelpers 或其他一些库)来完成此操作。有什么想法吗?
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
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;
}
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); }
}
}
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用数组字段,库将完成这项工作:
您甚至可以使用 [FieldArrayLength(2, 8)]
设置最小/最大值数
我强烈建议下载最新版本库的位置:
http://teamcity.codebetter.com/viewType .html?buildTypeId=bt65&tab=buildTypeStatusDiv
检查工件部分
You can use an Array Field and the library will do the work:
You can even use [FieldArrayLength(2, 8)]
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
Check the artifacts section
您可以创建一个类
MyRecord
来保存所有潜在值,例如,并使大多数字段可选(在我的示例中为 5 到 14),并将其与例如
FieldNullValue
结合起来code> 来处理那些不存在的字段(或者使这些可选字段成为可为 null 的 int:You could create a class
MyRecord
that holds all the potential values, e.g.and make most of those fields optional (no. 5 through 14 in my example) and combine that with e.g. a
FieldNullValue
to handle those non-existing fields (or make those optional fields a nullable int: