colinfang
colinfang

Reputation: 21757

F# csv provider with different column order

If I define a type

type MyType = CsvProvider<"schema.csv",
    Schema="A->MyA=int, B->MyB=int">

And if i load csv's like

let csv1 = MyType.Load("file1.csv")

If "file1.csv" contains all the columns that "schema.csv" has, but with different order, and have extra columns which do not appear in "schema.csv". Can I still load it provided that I am only interested in the columns that specified in "schema.csv"?

Upvotes: 1

Views: 267

Answers (2)

Helge Rene Urholm
Helge Rene Urholm

Reputation: 1188

Either you have a locked schema of the CSV-files, and use CsvProvider, or you dont.

You always have the option of "reverting" to CsvFile (CsvParser): http://fsharp.github.io/FSharp.Data/library/CsvFile.html

With the latter you can easily parse any CSV-file, confirm that it has the columns you want, and then read them as wanted.

I usually revert to the CsvFile, since often creating CSV-files are done somewhat unstructured and apperently ad-hoc (at least in the cases I have encountered), and then CsvFile are a good solution, with somewhat more flexibility then in CsvProvider. Yes somewhat more code too, but still...

Upvotes: 2

Gustavo Guerra
Gustavo Guerra

Reputation: 5359

That use case is not supported. If the column order is different things won't work. The whole CsvProvider is built on the assumption that the data you give it has the same structure of the sample you provided. You can always submit an issue here: https://github.com/fsharp/FSharp.Data/issues/

Upvotes: 1

Related Questions