在 F# 中使用 CsvProvider
using CsvProvider in F#
我是 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 <StartupCode$FSI_0044>.$FSI_0044.main@()
Stopped due to error
我的理解是,CsvProvider 创建了一个基于 CSV 文件的类型,这使得以后可以读取该文件或具有相同格式的不同 file/stream。
我想我没有目录问题,因为如果文件在错误的目录中,函数会 return 出错。
创建 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
>
我的猜测是 F# Interactive 很难在当前目录中找到 MSFT.csv
文件。您可以使用 Literal
:
指定相对于当前目录的完整路径
let [<Literal>] Sample = __SOURCE_DIRECTORY__ + "\MSFT.csv"
type Stocks = CsvProvider<Sample>
然后你可以调用Stocks.GetSample()
读取文件。
这种错误有时会在 F# Interactive 中发生,尤其是当您在不同文件夹中的文件之间切换时(我认为 F# Interactive 会记住上次执行命令的文件夹,但我认为它并不总是像预期的那样运行).
我是 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 <StartupCode$FSI_0044>.$FSI_0044.main@()
Stopped due to error
我的理解是,CsvProvider 创建了一个基于 CSV 文件的类型,这使得以后可以读取该文件或具有相同格式的不同 file/stream。 我想我没有目录问题,因为如果文件在错误的目录中,函数会 return 出错。 创建 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
>
我的猜测是 F# Interactive 很难在当前目录中找到 MSFT.csv
文件。您可以使用 Literal
:
let [<Literal>] Sample = __SOURCE_DIRECTORY__ + "\MSFT.csv"
type Stocks = CsvProvider<Sample>
然后你可以调用Stocks.GetSample()
读取文件。
这种错误有时会在 F# Interactive 中发生,尤其是当您在不同文件夹中的文件之间切换时(我认为 F# Interactive 会记住上次执行命令的文件夹,但我认为它并不总是像预期的那样运行).