0

次のコンソール プログラムを使用して、Csv 型プロバイダーの型情報 (データではなく) を取得したいと考えています。ファイル名は、コマンド ライン引数として渡されます。ただし、CsvProvider<>定数リテラルのみを受け入れるようです。

それを回避する方法はありますか?または、F# スクリプトを使用してそれを行うことは可能ですか? それとも、F# コンパイラ サービスが役に立ちますか?

または、これを行う他のプロジェクトはありますか?

open FSharp.Data
open Microsoft.FSharp.Collections
open System

[<Literal>] 
let fn = """C:\...\myfile.csv""" // Want to dynamically set the fn from arguments

[<EntryPoint>]
let main argv = 
    let myFile = CsvProvider<fn>.GetSample()
    // The following doesn't work
    let fn = argv.[0]
    let myFile = CsvProvider<fn>.GetSample()

    // code to get type information of myFile
4

2 に答える 2

4

I think you might be misunderstanding the purpose of the CSV type provider - the idea is that you have a representative sample of your data available at compile time (and can use it to guide the type inference). At runtime, you just give it (possibly a different) file with the same format. This gives you a nice way of handling files with known format.

If you want to parse arbitrary CSV files (with different headers etc.) then CSV type provider won't help. However, you can still use the CsvFile type from F# Data which provides a simple CSV parser. Example from the documentation:

// Download the stock prices
let msft = CsvFile.Load("http://ichart.finance.yahoo.com/table.csv?s=MSFT")

// Print the prices in the HLOC format
for row in msft.Rows do
  printfn "HLOC: (%s, %s, %s)" (row.GetColumn "High") 
     (row.GetColumn "Low") (row.GetColumn "Date")

Here, you loose the nice static typing, but you can load file with any format (and then dynamically look at the columns that were available in the file).

于 2015-06-03T04:59:43.467 に答える