Using CsvProvider in F #

I am new to F # and I am trying to use CsvProvider and reproduce the examples given here.

http://fsharp.imtqy.com/FSharp.Data/library/CsvProvider.html

so inside F # interactive I type

>type Stocks = CsvProvider<"MSFT.csv">;; type Stocks = CsvProvider<...> > let msft = CsvProvider<"MSFT.csv">.GetSample();; val msft : CsvProvider<...> > msft;; val it : CsvProvider<...> = FSharp.Data.Runtime.CsvFile`1[System.Tuple`1[System.String]] {Headers = Some [|"MSFT.csv"|]; NumberOfColumns = 1; Quote = '"'; Rows = seq []; Separators = ",";} > let firstRow = msft.Rows |> Seq.head;; System.ArgumentException: The input sequence was empty. Parameter name: source > at Microsoft.FSharp.Collections.SeqModule.Head[T](IEnumerable`1 source) at <StartupCode$FSI_0044> .$FSI_0044.main@ () Stopped due to error 

I understand that CsvProvider creates a type based on a CSV file, which allows you to read this file or another file / stream with the same format in the future. I think that I have no problem with the directory, because the function will return an error if the file was in the wrong directory. when msft is created, F # says NumberOfColumns = 1, but this is clearly wrong.

This does not work.

 > let msft = Stocks.Parse("MSFT.csv");; val msft : CsvProvider<...> > msft;; val it : CsvProvider<...> = FSharp.Data.Runtime.CsvFile`1[System.Tuple`1[System.String]] {Headers = Some [|"MSFT.csv"|]; NumberOfColumns = 1; Quote = '"'; Rows = seq []; Separators = ",";} 

I use the FSharp.Data library as follows: (is this a good version?)

 >#r "C:\Users\Fagui\Documents\GitHub\Learning Fsharp\Algo Stanford I\packages\FSharp.Data.2.2.5\lib\portable-net40+sl5+wp8+win8\FSharp.Data.dll" --> Referenced 'C:\Users\Fagui\Documents\GitHub\Learning Fsharp\Algo Stanford I\packages\FSharp.Data.2.2.5\lib\portable-net40+sl5+wp8+win8\FSharp.Data.dll' > open FSharp.Data;; > 

Please, help!!! I've tried it for hours! thanks!!!

EDIT : next magazine from F # interactive

 Microsoft (R) F# Interactive version 14.0.23020.0 Copyright (c) Microsoft Corporation. All Rights Reserved. For help type #help;; > #r "C:\Users\Fagui\Documents\GitHub\Learning Fsharp\Algo Stanford I\packages\FSharp.Data.2.2.5\lib\portable-net40+sl5+wp8+win8\FSharp.Data.dll" open FSharp.Data;; --> Referenced 'C:\Users\Fagui\Documents\GitHub\Learning Fsharp\Algo Stanford I\packages\FSharp.Data.2.2.5\lib\portable-net40+sl5+wp8+win8\FSharp.Data.dll' > open FSharp.Data;; > #I "C:\Users\Fagui\Documents\GitHub\Learning Fsharp\Algo Stanford I";; --> Added 'C:\Users\Fagui\Documents\GitHub\Learning Fsharp\Algo Stanford I' to library include path > let msft = CsvProvider<"MSFT.csv">.GetSample();; val msft : CsvProvider<...> > msft;; val it : CsvProvider<...> = FSharp.Data.Runtime.CsvFile`1[System.Tuple`1[System.String]] {Headers = Some [|"MSFT.csv"|]; NumberOfColumns = 1; Quote = '"'; Rows = seq []; Separators = ",";} > let msft = CsvProvider<"C:\Users\Fagui\Documents\GitHub\Learning Fsharp\Algo Stanford I\MSFT.csv">.GetSample();; let msft = CsvProvider<"C:\Users\Fagui\Documents\GitHub\Learning Fsharp\Algo Stanford I\MSFT.csv">.GetSample();; ---------------------------------------------------------------------------------------------------^^^^^^^^^ stdin(7,100): error FS0039: The field, constructor or member 'GetSample' is not defined > let msft = CsvProvider<"MSFT.csv">.GetSample();; val msft : CsvProvider<...> > let msft = CsvProvider<"C:\Users\Fagui\Documents\GitHub\Learning Fsharp\Algo Stanford I\MSFT.csv">.GetSample();; let msft = CsvProvider<"C:\Users\Fagui\Documents\GitHub\Learning Fsharp\Algo Stanford I\MSFT.csv">.GetSample();; ---------------------------------------------------------------------------------------------------^^^^^^^^^ stdin(9,100): error FS0039: The field, constructor or member 'GetSample' is not defined > 
+5
source share
1 answer

I assume that F # Interactive is having difficulty finding the MSFT.csv file in the current directory. You can specify the full path relative to the current directory using Literal :

 let [<Literal>] Sample = __SOURCE_DIRECTORY__ + "\\MSFT.csv" type Stocks = CsvProvider<Sample> 

You can then call Stocks.GetSample() to read the file.

Such an error sometimes occurs in F # Interactive, especially when switching between files in different folders (I think that F # Interactive remembers the folder of the last command executed, but I think that it does not always behave as one would expect).

+7
source

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


All Articles