简体   繁体   English

尝试加载示例文件时CsvProvider错误“给定密钥在字典中不存在”的含义?

[英]Meaning of CsvProvider error “The given key was not present in the dictionary” when trying to load sample file?

I am having trouble loading the csv files with FSharp.Data csv provider provided by fslab, including the sample adwords.csv file. 我在使用fslab提供的FSharp.Data csv提供程序加载csv文件时遇到问题,包括示例adwords.csv文件。

What does this error below mean? 下面这个错误是什么意思? Also, when I hover over the code in the Visual studio editor it mentions that "The given key was not present in the dictionary" 此外,当我将鼠标悬停在Visual Studio编辑器中的代码上时,它提到“给定的密钥不在字典中”

Problem example: 问题示例:

#load "packages/FsLab/FsLab.fsx"

open System.IO
open FSharp.Data

"adwords.csv"
|> File.ReadAllLines

let test = CsvProvider<"adwords.csv">.GetSample()

The output: 输出:

>
val it : string [] =
 [|"Criteria ID,Name,Canonical Name,Parent ID,Country Code,Target Type,Status";
    "1000010,Abu Dhabi,"Abu Dhabi,Abu Dhabi,United Arab Emirates",9041082,AE,City,Active";
"1000011,Ajman,"Ajman,Ajman,United Arab Emirates",9047096,AE,City,Active";
"1000012,Al Ain,"Al Ain,Abu Dhabi,United Arab Emirates",9041082,AE,City,Active";
"1000013,Dubai,"Dubai,Dubai,United Arab Emirates",9041083,AE,City,Active";
"2004,Afghanistan,Afghanistan,,AF,Country,Active"|]
>
>System.MethodAccessException: Attempt by method '<StartupCode$FSI_0007>.$FSI_0007.main@()' to access method 'FSharp.Data.Runtime.CsvFile`1<System.__Canon>.Create(System.Func`3<System.Object,System.String[],System.__Canon>,
at <StartupCode$FSI_0007>.$FSI_0007.main@() in C:\test.fsx:line 11
Stopped due to error

I ran into this problem with my own files, so I grabbed this sample file from here: https://raw.githubusercontent.com/fsharp/FSharp.Data/master/tests/FSharp.Data.Tests/Data/Adwords.csv 我用自己的文件遇到了这个问题,所以我从这里抓取了这个示例文件: https//raw.githubusercontent.com/fsharp/FSharp.Data/master/tests/FSharp.Data.Tests/Data/Adwords.csv

Debug info: 调试信息:

  • If I delete the FSharp.Data library folder (v 2.3.0) and replace with version 2.2.5 it works correctly with no error. 如果我删除FSharp.Data库文件夹(v 2.3.0)并替换为2.2.5版,它可以正常工作,没有错误。
  • If I don't use the FsLab.fsx script and instead use 如果我不使用FsLab.fsx脚本而是使用

    #I "packages/FSharp.Data/lib/net40 #r "FSharp.Data.dll" then everything works. #I "packages/FSharp.Data/lib/net40 #r "FSharp.Data.dll"然后一切正常。

  • The path to the FsLab.fsx script is correct, it runs when I send the line to fsi. FsLab.fsx脚本的路径是正确的,它在我将该行发送到fsi时运行。
  • The F# version is 14.0.23413.0. F#版本是14.0.23413.0。
  • The version of FSharp.Data downloaded by FSlab is FSharp.Data.2.3.0. FSlab下载的FSharp.Data版本为FSharp.Data.2.3.0。
  • I have no other references in the .fsx script. 我在.fsx脚本中没有其他引用。
  • I am using Visual Studio Community edition 14.0.24720.00 Update 1. 我正在使用Visual Studio Community Edition 14.0.24720.00 Update 1。
  • .NET version 4.6.01038 .NET版本4.6.01038
  • I am realizing now that I am not getting the popup asking if I want to allow the .dll like I think I used to get when I used this before. 我现在意识到,我没有得到弹出窗口,询问我是否想要允许.dll,就像我之前用过的那样。

There is nothing wrong with the file. 该文件没有任何问题。 This for example works: 这例如有效:

#load @"..\..\FSLAB\packages\FsLab\FsLab.fsx"

open System.IO
open FSharp.Data

[<Literal>]
let csvFile = @"C:\tmp\adwords.csv"
File.Exists csvFile

type Csv = CsvProvider<csvFile>
let csv = Csv.Load(csvFile)
csv.Rows

There is something wrong with your FsLab of FSharp.Data installation or type providers security maybe. 您的FsLab FSharp.Data安装或类型提供程序安全性可能有问题。 Try the following, specify the path to the file directly. 请尝试以下操作,直接指定文件的路径。 If it still doesn't work just nuget FSharp.Data and try using the csv type provider directly in a new project. 如果它仍然不起作用只是nuget FSharp.Data并尝试直接在新项目中使用csv类型提供程序。

Other info is also helpful. 其他信息也很有帮助。 VS version, FSLab version, wha other references you have. VS版本,FSLab版本,您有其他参考。 etc. 等等

EDIT: Thanks for the debug info. 编辑:感谢您的调试信息。 That's actually quite helpful. 这实际上非常有帮助。 VS2015 Update 1 broke two things, the Binding Redirect for Fsharp and the type providers (that might have been FSharp Tools, I forgot). VS2015 Update 1打破了两件事,Fsharp的绑定重定向和类型提供程序(可能是FSharp Tools,我忘了)。 I would upgrade to Update 2. If that's not possible please check if your FSharp.Data.TypeProviders.dll is in C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\FSharp\\.NETFramework\\v4.0\\4.3.0.0\\Type Providers . 我将升级到Update 2.如果不可能,请检查您的FSharp.Data.TypeProviders.dll是否在C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\FSharp\\.NETFramework\\v4.0\\4.3.0.0\\Type Providers

As referencing the dlls directly works, it's probably a version mismatch issue. 由于直接引用dll有效,可能是版本不匹配问题。 My FsLab install predates VS2015 Update 1 and 2, so will see if it behaves differently with a new download. 我的FsLab安装早于VS2015 Update 1和2,因此将查看新下载的行为是否有所不同。

There is some issue with the installation of FSharp.Data currently bundled with FsLab (as of June 2016). 安装目前与FsLab捆绑的FSharp.Data存在一些问题(截至2016年6月)。 This issue is with version 2.3.0. 此问题与版本2.3.0有关。 If you instead use FSharp.Data 2.2.5 the code works as expected. 如果您改为使用FSharp.Data 2.2.5,代码将按预期工作。

Delete the packages/FSharp.Data folder and replace with version 2.2.5. 删除packages / FSharp.Data文件夹并替换为2.2.5版。 I did it from an old installation but you could do it from Nuget 我是从一个旧的装置做到的,但你可以从Nuget做到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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