2 people like it.

read CSV with .net FileHelpers library

example to use the open source .net FileHelpers library to read a CSV ( http://filehelpers.sourceforge.net/ ). csv example with no exception error field1,field2,field3 123,123,aaa 456,123,aaa example with exception where the functions returns an empty array field1,field2,field3 123,aaa,aaa 456,123,aaa

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
open FileHelpers  
[<DelimitedRecord(",")>]
[<IgnoreFirst(1)>] 
type CsvRecord =
    class
        val field1 : string
        val field2 : int
        val field3 : string
    end
 
let engine = new FileHelperEngine<CsvRecord>()

let res = 
   try
      engine.ReadFile("test.csv")
   with
      | :? FileHelpersException -> Array.empty<CsvRecord>
namespace FileHelpers
Multiple items
type DelimitedRecordAttribute =
  inherit TypedRecordAttribute
  new : delimiter:string -> DelimitedRecordAttribute
  member Separator : string with get, set

Full name: FileHelpers.DelimitedRecordAttribute

--------------------
DelimitedRecordAttribute(delimiter: string) : unit
Multiple items
type IgnoreFirstAttribute =
  inherit Attribute
  new : unit -> IgnoreFirstAttribute + 1 overload
  member NumberOfLines : int with get, set

Full name: FileHelpers.IgnoreFirstAttribute

--------------------
IgnoreFirstAttribute() : unit
IgnoreFirstAttribute(numberOfLines: int) : unit
type CsvRecord =
  val field1: string
  val field2: int
  val field3: string

Full name: Script.CsvRecord
CsvRecord.field1: string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
CsvRecord.field2: int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
CsvRecord.field3: string
val engine : FileHelperEngine<CsvRecord>

Full name: Script.engine
Multiple items
type FileHelperEngine =
  inherit FileHelperEngine<obj>
  new : recordType:Type -> FileHelperEngine + 1 overload

Full name: FileHelpers.FileHelperEngine

--------------------
type FileHelperEngine<'T (requires reference type)> =
  inherit EventEngineBase<'T>
  new : unit -> FileHelperEngine<'T> + 1 overload
  member AppendToFile : fileName:string * record:'T -> unit + 1 overload
  member ReadFile : fileName:string -> 'T[] + 1 overload
  member ReadFileAsDT : fileName:string -> DataTable + 1 overload
  member ReadFileAsList : fileName:string -> List<'T> + 1 overload
  member ReadStream : reader:TextReader -> 'T[] + 1 overload
  member ReadStreamAsDT : reader:TextReader -> DataTable + 1 overload
  member ReadStreamAsList : reader:TextReader * maxRecords:int -> List<'T>
  member ReadString : source:string -> 'T[] + 1 overload
  member ReadStringAsDT : source:string -> DataTable + 1 overload
  ...

Full name: FileHelpers.FileHelperEngine<_>

--------------------
FileHelperEngine(recordType: System.Type) : unit
FileHelperEngine(recordType: System.Type, encoding: System.Text.Encoding) : unit

--------------------
FileHelperEngine() : unit
FileHelperEngine(encoding: System.Text.Encoding) : unit
val res : CsvRecord []

Full name: Script.res
FileHelperEngine.ReadFile(fileName: string) : CsvRecord []
FileHelperEngine.ReadFile(fileName: string, maxRecords: int) : CsvRecord []
Multiple items
type FileHelpersException =
  inherit Exception
  new : message:string -> FileHelpersException + 2 overloads

Full name: FileHelpers.FileHelpersException

--------------------
FileHelpersException(message: string) : unit
FileHelpersException(message: string, innerEx: exn) : unit
FileHelpersException(line: int, column: int, message: string) : unit
module Array

from Microsoft.FSharp.Collections
val empty<'T> : 'T []

Full name: Microsoft.FSharp.Collections.Array.empty

More information

Link:http://fssnip.net/aI
Posted:12 years ago
Author:Joeri Belis
Tags: .net library