Reading in a .csv file

Yes, it is my favorite subject, which is transforming data in .csv files. Here’s reading data into a variable in F#.


open System
open System.Threading
open System.Collections.Generic
open System.Linq
open System.Text
open System.Threading.Tasks
open System.IO
open Microsoft.VisualBasic.FileIO

[<EntryPoint>]
let main argv =
  let csv_fileH = new TextFieldParser(test1.csv)
  csv_fileH.TextFieldType = FieldType.Delimited |> ignore
  let x = csv_fileH.SetDelimiters(',')
  let csv_data = new List string[]()

  let eod = csv_fileH.EndOfData

  if not eod then
    let column_headings = csv_fileH.ReadFields()
    csv_data.Add(column_headings) |> ignore

    // Parentheses are needed after function definition to produce type bool.
    let read_rest_of_csv() =
    csv_data.Add(csv_fileH.ReadFields()) |>
    not csv_fileH.EndOfData

    while read_rest_of_csv() do ignore None

  0 // return an integer exit code

This program is associated on stackoverflow with some very good answers to a question I asked.

Advertisements

Leave a comment

Filed under F#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s