How to create a CSV file and write data to f #

How can I create a csv file in f sharp and write the following record type to it?

type test = { G:array<double>; P:array<double>; GG:array<double>; PP:array<double> } let table = [for x in 0..(Un0.Length - 1) -> let b = Un0.[x] in if b=0.0 then {G=0.0; P=0.0; GG=0.0; PP=0.0} else {G=G_0.[x]/b; P=P0.[x]/b; GG=G0.[x]/b; PP=PP0.[x]/b}] 
+5
source share
3 answers

There is no need to use F # data to write to .csv.

I changed the test definition and added some values โ€‹โ€‹so that you can compile:

 type test = { G:double; P:double; GG:double; PP:double } override this.ToString() = sprintf "%f;%f;%f;%f\n" this.G this.P this.GG this.PP let G_0 = [|(0.0)..(10.0)|] let Un0 = [|(1.0)..(11.0)|] let P0 = [|(2.0)..(12.0)|] let G0 = [|(3.0)..(13.0)|] let PP0 = [|(4.0)..(14.0)|] let table = [for x in 0..(Un0.Length - 1) -> let b = Un0.[x] if b=0.0 then {G=0.0; P=0.0; GG=0.0; PP=0.0} else {G=G_0.[x]/b; P=P0.[x]/b; GG=G0.[x]/b; PP=PP0.[x]/b}] let wr = new System.IO.StreamWriter("Csv.csv") table |> List.map(string) |> String.concat("") |> wr.Write wr.Close() 

Result:

enter image description here

+4
source

the CSV type provider from FSharp.Data is primarily known and used to read CSV (as the name implies), but it is also quite capable of writing CSV.

All you have to do is determine the type, either by providing a sample .CSV file

 let titanic2 = CsvProvider<"../data/Titanic.csv", Schema="Fare=float,PClass->Passenger Class">.GetSample() 

or by directly defining the circuit

 type MyCsvType = CsvProvider<Schema = "A (int), B (string), C (date option)", HasHeaders=false> 

then you can create a recording object and fill it (in a safe way!)

 // you can build the rows themselves let myCsv = new MyCsvType( [ MyCsvType.Row(1, "a", None) MyCsvType.Row(2, "B", Some DateTime.Now) ]) // or, for your scenario, you probably want to define a conversion function // from your record type to the CSV provider type let buildRowFromObject obj = MyCsvType.Row(obj.A, obj.B, obj.C) let buildTableFromObjects = (Seq.map buildRowFromObject) >> Seq.toList >> MyCsvType let myCsv = someSequenceOfObjects |> buildTableFromObjects 

and finally just call

 myCsv.SaveToString() 

to get the result in CSV format.

+10
source

This is not exactly the answer to your question, but it is similar and may be convenient

How to modify csv file using f # CSV type provider

Decision:

 #r "..\packages\FSharp.Data.2.3.3\lib\\net40\FSharp.Data.dll" open System.IO open FSharp.Data type CsvTest = CsvProvider<"""C:\Users\User\Desktop\sample.csv"""> let textReader = File.OpenRead("""C:\Users\User\Desktop\sample.csv""")// need to use TextReader let csvTest = CsvTest.Load(textReader) let modified = csvTest.Append [CsvTest.Row(3,"bum") ]// add row to csv textReader.Close()// closing file before edit... modified.Save """C:\Users\User\Desktop\sample.csv""" //save it 

You may need a Reset Interactive Session (if you are using fsx)

Explanation:

Simple file upload:

 let csvTest = CsvTest.Load("""C:\Users\User\Desktop\sample.csv""") 

Does not work. If you try, you will get an error message:

System.IO.IOException: "The process cannot access the file" C: \ Users \ User \ Desktop \ sample.csv "because it is being used by another process.

Therefore, I use TextReader, which can be closed ..

Maybe there is the simplest solution, but I did not find it.

0
source

Source: https://habr.com/ru/post/1233504/


All Articles