Reading CSV file and storing values into an array

后端 未结 19 1469
猫巷女王i
猫巷女王i 2020-11-22 06:35

I am trying to read a *.csv-file.

The *.csv-file consist of two columns separated by semicolon (\";\").

I am able

19条回答
  •  灰色年华
    2020-11-22 06:58

    The open-source Angara.Table library allows to load CSV into typed columns, so you can get the arrays from the columns. Each column can be indexed both by name or index. See http://predictionmachines.github.io/Angara.Table/saveload.html.

    The library follows RFC4180 for CSV; it enables type inference and multiline strings.

    Example:

    using System.Collections.Immutable;
    using Angara.Data;
    using Angara.Data.DelimitedFile;
    
    ...
    
    ReadSettings settings = new ReadSettings(Delimiter.Semicolon, false, true, null, null);
    Table table = Table.Load("data.csv", settings);
    ImmutableArray a = table["double-column-name"].Rows.AsReal;
    
    for(int i = 0; i < a.Length; i++)
    {
        Console.WriteLine("{0}: {1}", i, a[i]);
    }
    

    You can see a column type using the type Column, e.g.

    Column c = table["double-column-name"];
    Console.WriteLine("Column {0} is double: {1}", c.Name, c.Rows.IsRealColumn);
    

    Since the library is focused on F#, you might need to add a reference to the FSharp.Core 4.4 assembly; click 'Add Reference' on the project and choose FSharp.Core 4.4 under "Assemblies" -> "Extensions".

提交回复
热议问题