简体   繁体   中英

using CsvProvider in F#

I'm a beginner in F# and I'm trying to use CsvProvider and to reproduce the examples given here

http://fsharp.github.io/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

My understanding is that the CsvProvider creates a type based on a CSV file, which enables to read that file or a different file/stream with the same format later on. i think i have no directory problem, as the function would return an error if the file was in the wrong directory. when msft is created, F# says the NumberOfColumns= 1 but thats obviously wrong.

This doesn't work either

> 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 am using the FSharp.Data library like this: (is it the 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 been trying for hours now ! thanks !!!

EDIT : the following is the whole log 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
> 

My guess is that F# Interactive has a difficulty in finding the MSFT.csv file in the current directory. You can specify a full path relative to the current directory using a Literal :

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

Then you can call Stocks.GetSample() to read the file.

This kind of error sometimes happens in F# Interactive, especially when you switch between files in different folders (I think F# Interactive remembers the folder of the last executed command, but I think it does not always behave as one would expect).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM