gpt4 book ai didi

csv - 在 F# 中使用 CsvProvider

转载 作者:行者123 更新时间:2023-12-04 14:25:41 25 4
gpt4 key购买 nike

我是 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 文件创建一个类型,这样以后就可以读取该文件或具有相同格式的不同文件/流。
我认为我没有目录问题,因为如果文件位于错误的目录中,该函数将返回错误。
创建 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 会记住上次执行命令的文件夹,但我认为它的行为并不总是像预期的那样)。

关于csv - 在 F# 中使用 CsvProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34629299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com