How can i can i create a csv file in f sharp and write the following record type in 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}]
To record in .csv is not necessary to use F# Data.
I changed the definition of test and added some values so that you can compile:
Result:
The CSV type provider from FSharp.Data is primarily known and used for reading CSVs (as the name suggests), but it's also quite capable of writing CSVs as well.
All you need to do is to define the type, either by providing a sample .CSV file
or by directly defining the schema
then you can create a record object and populate it (in a type-safe way!)
and finally, just call
to get the output in CSV format.
This is not exactly answer to your question, but it is similar and could be handy
How to EDIT csv file with f# CSV Type Provider
Solution:
You may need to Reset Interactive Session (if you work with fsx)
Explanation:
Straightforward file loading like this:
Does not work. If you try it you will get error:
So I use TextReader, that can be closed..
Maybe there is easiest solution, but I didn't find any.