Giter VIP home page Giter VIP logo

csvhelperfs's Introduction

CsvHelperfs

thin fsharp's CsvHelper's wrapper

Usage

A.Read csv

  1. open CsvHelperfs at main code
  2. customize CsvRead0.csv and input.jsonc (see Sample folder)
  3. write code at main code - like blow...
module Foo =

  open System.IO
  open System.Text
  open System.Text.Json
  open CsvHelperfs

  async {

    let fp_jsonc   = "./Sample/input.jsonc"

    // read Config from Json
    let csvReadInfo =
      use stream = new FileStream(fp_jsonc,FileMode.Open,FileAccess.Read)
      let opt = JsonSerializerOptions()
      opt.ReadCommentHandling <- JsonCommentHandling.Skip
      JsonSerializer.Deserialize<CsvHelperfs.CsvReadInfo>(stream, opt)

    // read csv
    let folderPath       = csvReadInfo.InputFileConfig.InputFolderName
    let filePath         = csvReadInfo.InputFileConfig.InputFileName
    let finfo            = FileInfo(Path.Combine(Directory.GetCurrentDirectory(), folderPath, filePath))
    let codepage         = csvReadInfo.InputFileConfig.CodePage.Value
    use sr               = new StreamReader(finfo.FullName,Encoding.GetEncoding(codepage))
    let! csvBad, csvGood = CsvRead0.readCsv csvReadInfo finfo sr

    printfn "%A" csvBad
    printfn "%A" csvGood

    // write csv of error log
    let filePath' = csvReadInfo.InputErrorFileConfig.OutputFileConfig.OutputFileName
    let fp_errCsv = Path.Combine(Directory.GetCurrentDirectory(),filePath')
    do! csvBad |> Seq.toArray |> CsvWrite.csvWriteFromRecordArray csvReadInfo.InputErrorFileConfig fp_errCsv

  }
  |> Async.RunSynchronously

output(console)

seq [{ FileName = "fruits.csv"
       LastWriteTime = 2023/05/03 18:51:48 
       RowNumberPhysical = 4
       RowNumberLogical = 4
       Field = ""
       Message1 = " Id is invalid(over 3) "
       Message2 = ""
       Record = "3,cherry,500,black"       
       Memo = "" }]

seq [{ Id = 1
       Name = "apple"
       Price = 300
       Memo = "red" }; { Id = 2
                         Name = "banana"   
                         Price = 250       
                         Memo = "yellow" }]

output(CsvError.csv)

"FileName","LastWriteTime","RowNumberPhysical","RowNumberLogical","Field","Message1","Message2","Record","Memo"
"fruits.csv","2023/05/04 17:17:25","4","4",""," Id is invalid(over 3) ","","3,cherry,500,black",""

B.Write csv

  1. open CsvHelperfs at main code
  2. customize output.jsonc (see Sample folder)
  3. write code at main code - like blow...
module Foo =

  open System.Collections.Generic
  open System.IO
  open System.Dynamic
  open System.Text.Json
  open CsvHelperfs

  async {

    // date is DapperRow:IDictionary or ExpandoObject.IDictionary
    let idic =
      seq {
        Map([("name",box "cat")   ; ("size",box "small") ]) |> Map.toSeq |> dict
        Map([("name",box "horse") ; ("size",box "big") ])   |> Map.toSeq |> dict
      }
      |> Seq.map CsvUtil.asExpandoIDictionary
      |> Seq.toArray

    let fp_jsonc   = "./Sample/output.jsonc"

    // read Config from Json
    let csvWriteInfo =
      use stream = new FileStream(fp_jsonc,FileMode.Open,FileAccess.Read)
      let opt = JsonSerializerOptions()
      opt.ReadCommentHandling <- JsonCommentHandling.Skip
      JsonSerializer.Deserialize<CsvHelperfs.CsvWriteInfo>(stream, opt)

    // write csv
    let filePath = csvWriteInfo.OutputFileConfig.OutputFileName
    let finfo    = FileInfo(Path.Combine(Directory.GetCurrentDirectory(),filePath))
    do! CsvWrite.csvWriteFromExpandoIDictionaryArray csvWriteInfo finfo.FullName idic

  }
  |> Async.RunSynchronously

output(animal.csv)

"name","size"
"cat","small"
"horse","big"

C. Extra Config

module Foo =

  open System.IO
  open System.Text.Json

  // wrap dummy record if read records directly from json
  type ExtraInputJson = {ExtraInput:ExtraInput}
  and ExtraInput =
    {
      ExtraInputFileConfig      : ExtraInputFileConfig
      ExtraInputErrorFileConfig : ExtraInputErrorFileConfig
    }
  and ExtraInputFileConfig =
    {
      CheckCsvFiles        : bool
      LimitRecords         : Nullable<int>
      IgnoreInvalidRecords : bool
    }
  and ExtraInputErrorFileConfig =
    {
      OutputFileNameDateFormat : string
      IsPrefixDateFormat       : bool
    }

  async{

    let fp_jsonc   = "./Sample/input.jsonc"

    // read Config from Json
    let extraInput =
      use stream = new FileStream(fp_jsonc,FileMode.Open,FileAccess.Read)
      let opt = JsonSerializerOptions()
      opt.ReadCommentHandling <- JsonCommentHandling.Skip
      JsonSerializer.Deserialize<ExtraInputJson>(stream, opt) |> fun a -> a.ExtraInput

    extraInput.ExtraInputFileConfig.LimitRecords.Value |> printfn "%d"

  }
  |> Async.RunSynchronously

output(console)

9999

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.