我是F#的初学者,我正在尝试使用CsvProvider并重现这里给出的例子
http://fsharp.github.io/FSharp.Data/library/CsvProvider.html
所以在F#互动中,我打字
>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.$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 >
Tomas Petric.. 8
我的猜测是F#Interactive在查找MSFT.csv
当前目录中的文件时遇到了困难.您可以使用以下命令指定相对于当前目录的完整路径Literal
:
let [] Sample = __SOURCE_DIRECTORY__ + "\\MSFT.csv" type Stocks = CsvProvider
然后你可以打电话Stocks.GetSample()
来读取文件.
这种错误有时会发生在F#Interactive中,尤其是当您在不同文件夹中的文件之间切换时(我认为F#Interactive会记住上次执行的命令的文件夹,但我认为它并不总是像人们期望的那样).
我的猜测是F#Interactive在查找MSFT.csv
当前目录中的文件时遇到了困难.您可以使用以下命令指定相对于当前目录的完整路径Literal
:
let [] Sample = __SOURCE_DIRECTORY__ + "\\MSFT.csv" type Stocks = CsvProvider
然后你可以打电话Stocks.GetSample()
来读取文件.
这种错误有时会发生在F#Interactive中,尤其是当您在不同文件夹中的文件之间切换时(我认为F#Interactive会记住上次执行的命令的文件夹,但我认为它并不总是像人们期望的那样).