在F#中使用CsvProvider

时间:2016-01-06 09:11:10

标签: csv io f# f#-data

我是F#的初学者,我尝试使用CsvProvider并重现此处给出的示例

http://fsharp.github.io/FSharp.Data/library/CsvProvider.html

所以在F#interactive中,我输入

>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

我的理解是CsvProvider基于CSV文件创建一个类型,该文件允许稍后以相同的格式读取该文件或不同的文件/流。 我认为我没有目录问题,因为如果文件位于错误的目录中,该函数将返回错误。 当创建msft时,F#表示NumberOfColumns = 1,但这显然是错误的。

这不起作用

> 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 = ",";}

我正在使用这样的FSharp.Data库:(这是好的版本吗?)

>#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;;
> 

请帮忙!!!我现在已经尝试了好几个小时了!谢谢!!!

编辑:以下是来自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
> 

1 个答案:

答案 0 :(得分:8)

我的猜测是F#Interactive在查找当前目录中的MSFT.csv文件时遇到了困难。您可以使用Literal

指定相对于当前目录的完整路径
let [<Literal>] Sample = __SOURCE_DIRECTORY__ + "\\MSFT.csv"
type Stocks = CsvProvider<Sample>

然后你可以致电Stocks.GetSample()来阅读文件。

这种错误有时会发生在F#Interactive中,尤其是当您在不同文件夹中的文件之间切换时(我认为F#Interactive会记住上次执行的命令的文件夹,但我认为它并不总是像人们期望的那样)。