filehelpers-分析可变行长度

本文关键字:filehelpers- | 更新日期: 2023-09-27 17:58:31

我必须解析(C#)一个.CSV文件,该文件具有变量"宽度"和两行标题信息(第一行是名称,第二行是单位)。

数据看起来像:

示例1.CSV:

"timestamp","NAME_1","NAME_2","NAME_3","NAME_4"
"ms","unit_1","unit_2","unit_3","unit_4"
0.01,1.23,4.56,7.89,0.12
0.02,1.23,4.66,7.89,0.11
0.03,1.23,4.76,7.89,0.11
0.04,56.23,4.86,7.89,0.12

示例2.CSV:

"timestamp","NAME_1","NAME_2","NAME_3","NAME_4","NAME_5",...,"NAME_N"
"ms","unit_1","unit_2","unit_3","unit_4","unit_5",...,"unit_N"
0.01,1.23,4.56,7.89,0.12,0.13,...,0.27
0.02,1.23,4.66,7.89,0.12,0.13,...,0.22
0.03,1.23,4.76,7.89,0.11,0.13,...,0.24
0.04,56.23,4.86,7.89,0.12,0.13,...,0.29

其中N是表的"宽度"(值最多可以是128或更大)。我计划使用Filehelpers。

我曾想过使用[FieldOptional()],但这很不愉快,尤其是当"宽度"可变时。。。

我当前的尝试看起来像

[IgnoreFirst(2)]
[DelimitedRecord(",")]
public sealed class LogData
{
    public Double ts;
    public Double Field1;
    [FieldNullValue(0.0)]
    [FieldOptional()]
    public Double Field2;
    [FieldNullValue(0.0)]
    [FieldOptional()]
    public Double Field3;
    // and so on
}

任何关于"如何解决可变宽度"的帮助-以更优雅的方式解决问题,不胜感激-非常感谢!

Ben

filehelpers-分析可变行长度

如果您计划将文件转换为DataTable,有一个更好的选项

请使用FileHelpers库的CsvEngine。请参阅下面的代码片段:

using (MemoryStream stream = new MemoryStream(_fileContent)) //file content can be file as byte array
            {
                TextReader reader = new StreamReader(stream);
string path = "C:''Sample.csv";
                CsvEngine csvEngine = new CsvEngine("Model", ',', path);
                var dataTable = csvEngine.ReadStreamAsDT(reader);
//Do whatever with dataTable
}

这里的示例文件可以是csv文件,也可以是包含要处理的csv文件头的文本文件。DataTable的列将根据样本文件的标题进行命名

干杯

您可以使用可选的数组字段。我认为您需要使用FileHelpers 2.9.9。

[IgnoreFirst(2)]
[DelimitedRecord(",")]
public class LogData
{
    public Double TimeStamp;
    [FieldNullValue(0.0)]
    [FieldOptional, FieldArrayLength(0, 100)]
    public Double[] ManyFields;
}

下面是一个工作示例。

class Program
{
    static String content = 
         @"""timestamp"",""NAME_1"",""NAME_2"",""NAME_3"",""NAME_4""
         ""ms"",""unit_1"",""unit_2"",""unit_3"",""unit_4""
         0.01,1.23,4.56,7.89,0.12
         0.02,1.23,4.66,7.89,0.11
         0.03,1.23,4.76,7.89,0.11
         0.04,56.23,4.86,7.89,0.12";
    private static void Main()
    {
        var engine = new FileHelperEngine<LogData>();
        var records = engine.ReadString(content);
        Assert.AreEqual(0.01, records[0].TimeStamp);
        Assert.AreEqual(1.23, records[0].ManyFields[0]);
        Assert.AreEqual(4.56, records[0].ManyFields[1]);
        Assert.AreEqual(7.89, records[0].ManyFields[2]);
        Assert.AreEqual(0.12, records[0].ManyFields[3]);
    }
}