adfglkj
adfglkj

Reputation: 395

Reading tab delimited files in f#

I'm new to F# and don't have a lot of experience with functional languages. I need to select the values in a couple of columns of a tab delimited file. I've found how to read lines of a text file into sequences, but sequences don't seem to work like arrays and I can't figure out how to select a specific value from them. Can anyone help? Thanks.

Upvotes: 2

Views: 675

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243051

You should be able to use the CSV type provider to do this. It supports tab-delimited files too (see the "Custom separators and tab-separated files" section on that page).

To use the type provider, you'll need a sample (which could also be your actual input file). Then you can tell the type provider to infer a type based on your sample. If you have tsv extension, it automatically treats it as tab-separated, but you can also specify separators explicitly:

type MyFormat = CsvProvider<"C:/sample.tsv", Separators="\t">

Then you can use the inferred type to read your data:

let data = MyFormat.Load("C:/mydata.tsv")
for row in data.Rows do
  printfn "%s" row.YourColumn

Upvotes: 2

Related Questions