本文介绍了FileHelpers和CSV:该怎么办时,一个记录可以无限扩展的,水平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图分析这种类型的CSV文件与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

我无法弄清楚如何与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.

我缺少的东西?我已经讨论了文档/例子,但似乎无法找到我的格式的解决方案。 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?

推荐答案

您可以使用阵列字段和图书馆将做的工作:

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:

<一个href="http://teamcity.$c$cbetter.com/viewType.html?buildTypeId=bt65&tab=buildTypeStatusDiv">http://teamcity.$c$cbetter.com/viewType.html?buildTypeId=bt65&tab=buildTypeStatusDiv

检查工件截面

这篇关于FileHelpers和CSV:该怎么办时,一个记录可以无限扩展的,水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:41