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}]
Steps for writing a CSV file First, open the CSV file for writing ( w mode) by using the open() function. Second, create a CSV writer object by calling the writer() function of the csv module. Third, write data to CSV file by calling the writerow() or writerows() method of the CSV writer object.
writer() function. The csv. writer() function returns a writer object that converts the user's data into a delimited string. This string can later be used to write into CSV files using the writerow() function.
Reading from CSV file At first, the CSV file is opened using the open() method in 'r' mode(specifies read mode while opening a file) which returns the file object then it is read by using the reader() method of CSV module that returns the reader object that iterates throughout the lines in the specified CSV document.
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 then you can create a record object and populate it (in a type-safe way!)
Create object of File class and pass the CSV file name as parameter. Create an object of FileWrite r class and pass the File class object as parameter. Then write data into file as comma-separated values in each line by using for loop. Check the file has been created in the respective path and open it, you will see the data in it.
Second, create a CSV writer object by calling the writer () function of the csv module. Third, write data to CSV file by calling the writerow () or writerows () method of the CSV writer object. Finally, close the file once you complete writing data to it.
Spreadsheets often export CSV (comma seperated values) files, because they are easy to read and write. A csv file is simply consists of values, commas and newlines. While the file is called ‘comma seperate value’ file, you can use another seperator such as the pipe character.
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
let titanic2 = CsvProvider<"../data/Titanic.csv", Schema="Fare=float,PClass->Passenger Class">.GetSample()
or by directly defining the schema
type MyCsvType = CsvProvider<Schema = "A (int), B (string), C (date option)", HasHeaders=false>
then you can create a record object and populate it (in a type-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's 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 output in CSV format.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With