FileHelpers 和 CSV:当记录可以无限水平扩展时该怎么办

发布于 2024-08-23 21:33:07 字数 898 浏览 5 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的背包 2024-08-30 21:33:07

您可以使用数组字段,库将完成这项工作:

[DelimitedRecord(",")]
public class MyRecord
{
    public string Name;

    public int[] Values;
}

您甚至可以使用 [FieldArrayLength(2, 8)]

[DelimitedRecord(",")]
public class MyRecord
{
    public string Name;

    [FieldArrayLength(2, 8)]
    public int[] Values;
}

设置最小/最大值数

我强烈建议下载最新版本库的位置:

http://teamcity.codebetter.com/viewType .html?buildTypeId=bt65&tab=buildTypeStatusDiv

检查工件部分

You can use an Array Field and the library will do the work:

[DelimitedRecord(",")]
public class MyRecord
{
    public string Name;

    public int[] Values;
}

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

Check the artifacts section

恋你朝朝暮暮 2024-08-30 21:33:07

您可以创建一个类 MyRecord 来保存所有潜在值,例如,

[DelimitedRecord(",")]
public class MyRecord
{
    public string Name;
    public int Value1;
    .....
    [FieldOptional]
    public int Value5;
    ......
    [FieldOptional]
    [FieldNullValue(typeof(int), "-1" )]
    public int Value14;
}

并使大多数字段可选(在我的示例中为 5 到 14),并将其与例如 FieldNullValue 结合起来code> 来处理那些不存在的字段(或者使这些可选字段成为可为 null 的 int:

 public int? Value5

You could create a class MyRecord that holds all the potential values, e.g.

[DelimitedRecord(",")]
public class MyRecord
{
    public string Name;
    public int Value1;
    .....
    [FieldOptional]
    public int Value5;
    ......
    [FieldOptional]
    [FieldNullValue(typeof(int), "-1" )]
    public int Value14;
}

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:

 public int? Value5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文