使用不带类型的FileHelpers [英] Using FileHelpers without a type

查看:96
本文介绍了使用不带类型的FileHelpers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在从另一个系统导出的CSV文件,列顺序和定义可能会更改.我发现FileHelpers非常适合读取csv文件,但是除非您在编译应用程序之前了解列的顺序,否则您似乎无法使用它.我想知道是否有可能以非类型化方式使用FileHelpers.目前,我正在使用它来读取文件,但随后我要手动执行其他所有操作,所以我有一个课:

[DelimitedRecord(",")]
public class CSVRow
{
    public string Content { get; set; }
}

这意味着每一行都在Content之内,这很好,因为我随后将行拆分了,依此类推,但是由于文件中固有的逗号,我现在在此方法上遇到问题,所以一行可能是:

"something",,,,0,,1,,"something else","","",,,"something, else"

我对此字符串上的逗号进行简单的拆分不起作用,因为"something,else"中有一个逗号,该逗号被拆分了.显然,在这里像FileHelpers这样的地方非常有用,它可以解析这些值并考虑引号.因此,有可能以这种方式使用FileHelpers,而不必知道已知的列定义,或者至少能够将其传递给csv字符串并获取值列表,或者是否有任何好的库可以做到这一点?

解决方案

您可以使用 FileHelpers的运行时记录,如果您知道(或可以推断出)运行时列的顺序和定义.

否则,关于CSV库有很多问题,例如在C#中读取CSV文件

更新的链接.原始文档已存档此处

I have a CSV file that is being exported from another system whereby the column orders and definitions may change. I have found that FileHelpers is perfect for reading csv files, but it seems you cannot use it unless you know the ordering of the columns before compiling the application. I want to know if its at all possible to use FileHelpers in a non-typed way. Currently I am using it to read the file but then everything else I am doing by hand, so I have a class:

[DelimitedRecord(",")]
public class CSVRow
{
    public string Content { get; set; }
}

Which means each row is within Content, which is fine, as I have then split the row etc, however I am now having issues with this method because of commas inherent within the file, so a line might be:

"something",,,,0,,1,,"something else","","",,,"something, else"

My simple split on commas on this string doesnt work as there is a comma in `"something, else" which gets split. Obviously here is where something like FileHelpers comes in real handy, parsing these values and taking the quote marks into consideration. So is it possible to use FileHelpers in this way, without having a known column definition, or at least being able to pass it a csv string and get a list of values back, or is there any good library that does this?

解决方案

You can use FileHelpers' RunTime records if you know (or can deduce) the order and definitions of the columns at runtime.

Otherwise, there are lots of questions about CSV libraries, eg Reading CSV files in C#

Edit: updated link. Original is archived here

这篇关于使用不带类型的FileHelpers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆