F# CsvTypeProvider extracting the same columns from slightly different csv-files

severin

I am creating a program that reads football matches from different CSV files. The columns I am interested in are present in all the files, but the files have a varying number of columns.

This left me creating a separate mapping function for each variation of file, with a different sample for each type:

type GamesFile14 = CsvProvider<"./data/sample_14.csv">
type GamesFile15 = CsvProvider<"./data/sample_15.csv">
type GamesFile1617 = CsvProvider<"./data/sample_1617.csv">

let mapRows14 (rows:seq<GamesFile14.Row>) = rows |> Seq.map ( fun c -> { Division = c.Div; Date = DateTime.Parse c.Date; 
        HomeTeam = { Name = c.HomeTeam; Score = c.FTHG; Shots = c.HS; ShotsOnTarget = c.HST; Corners = c.HC; Fouls = c.HF }; 
        AwayTeam = { Name = c.AwayTeam; Score = c.FTAG; Shots = c.AS; ShotsOnTarget = c.AST; Corners = c.AC; Fouls = c.AF };
        Odds = { H = float c.B365H; U = float c.B365D;  B = float c.B365A } } ) 


let mapRows15 (rows:seq<GamesFile15.Row>) = rows |> Seq.map ( fun c -> { Division = c.Div; Date = DateTime.Parse c.Date; 
        HomeTeam = { Name = c.HomeTeam; Score = c.FTHG; Shots = c.HS; ShotsOnTarget = c.HST; Corners = c.HC; Fouls = c.HF }; 
        AwayTeam = { Name = c.AwayTeam; Score = c.FTAG; Shots = c.AS; ShotsOnTarget = c.AST; Corners = c.AC; Fouls = c.AF };
        Odds = { H = float c.B365H; U = float c.B365D;  B = float c.B365A } } ) 


let mapRows1617 (rows:seq<GamesFile1617.Row>) = rows |> Seq.map ( fun c -> { Division = c.Div; Date = DateTime.Parse c.Date; 
        HomeTeam = { Name = c.HomeTeam; Score = c.FTHG; Shots = c.HS; ShotsOnTarget = c.HST; Corners = c.HC; Fouls = c.HF }; 
        AwayTeam = { Name = c.AwayTeam; Score = c.FTAG; Shots = c.AS; ShotsOnTarget = c.AST; Corners = c.AC; Fouls = c.AF };
        Odds = { H = float c.B365H; U = float c.B365D;  B = float c.B365A } } ) 

These are again consumed by the loadGames function:

let loadGames season resource = 
    if season.Year = 14 then GamesFile14.Load(resource).Rows |> mapRows14
    else if season.Year = 15 then GamesFile15.Load(resource).Rows |> mapRows15
    else GamesFile1617.Load(resource).Rows |> mapRows1617

It seems to me that there must be better ways to get around this problem.

Is there any way I could make my mapping function more generic so that I don't have to repeat the same function over and over?

Is it possible to create the CsvProvider on the fly based on the resource, or do I need to explicitly declare a sample for each variation of my csv-files like in the code above?

Other suggestions?

rmunn

In your scenario, you might get better results from FSharp.Data's CsvFile type. It uses a more dynamic approach to CSV parsing, using the dynamic ? operator for data access: you lose some of the type-safety guarantees that the type provider gives you, since each separate CSV file will be loaded into the save CsvRow type -- which means that you can't guarantee at compile time that any given column will be in a file, and you have to be prepared for runtime errors. But in your case, that's just what you want, because it would allow your three functions to be rewritten like this:

let mapRows14 rows = rows |> Seq.map ( fun c -> { Division = c?Div; Date = DateTime.Parse c?Date; 
        HomeTeam = { Name = c?HomeTeam; Score = c?FTHG; Shots = c?HS; ShotsOnTarget = c?HST; Corners = c?HC; Fouls = c?HF }; 
        AwayTeam = { Name = c?AwayTeam; Score = c?FTAG; Shots = c?AS; ShotsOnTarget = c?AST; Corners = c?AC; Fouls = c?AF };
        Odds = { H = float c?B365H; U = float c?B365D;  B = float c?B365A } } )

Give CsvFile a try and see if it solves your problem.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Generate slightly different APKs from the same code

From Dev

BO WEBI : Extracting in two different columns different values from the same dimension

From Dev

Ruby how to merge two CSV files with slightly different headers

From Dev

Extracting files from windows to linux on a same partition

From Dev

F# CsvTypeProvider - mapping function from inferred types

From Dev

F# CsvTypeProvider - mapping function from inferred types

From Dev

Operations with columns from different files

From Dev

Copying different columns into one single column from different files and renaming them with the same filename

From Dev

Switch Columns in .csv files so that they are all the same

From Java

Trying to merge different files csv and to label the columns

From Dev

Merge two csv files horizontally with different columns

From Dev

Loop through .csv files with conditions in different columns

From Dev

Loop through .csv files with conditions in different columns

From Dev

R:Extracting words from one column into different columns

From Dev

Extracting columns from text file with different delimiters in Linux

From Dev

Transfer cell values from different columns and sheets from multiple excel files with same structure into a single dataframe

From Dev

Merge and compare different columns from different files

From Dev

How to merge xts objects with slightly different columns?

From Dev

Two CSV files with same column names,. I need to perform linear regression comparing columns with same header name from both files

From Dev

Extracting same lines from two files while disregarding lower/uppercase

From Dev

Extracting multiple data files from a single csv file

From Dev

Extracting information from multiple JSON files to single CSV file in python

From Dev

Diff: slightly different filenames, same file

From Dev

Same Method for Client and Server slightly different behavior

From Dev

Extracting columns from a file

From Dev

Extracting columns from a file

From Dev

Compare columns from different excel files and if match occurs, append to the last column(on the same line) a status

From Dev

How can I add columns from 2 different files to an output in CSV python

From Dev

Loop in R to get specific columns and the sum of the rest from different .csv files

Related Related

  1. 1

    Generate slightly different APKs from the same code

  2. 2

    BO WEBI : Extracting in two different columns different values from the same dimension

  3. 3

    Ruby how to merge two CSV files with slightly different headers

  4. 4

    Extracting files from windows to linux on a same partition

  5. 5

    F# CsvTypeProvider - mapping function from inferred types

  6. 6

    F# CsvTypeProvider - mapping function from inferred types

  7. 7

    Operations with columns from different files

  8. 8

    Copying different columns into one single column from different files and renaming them with the same filename

  9. 9

    Switch Columns in .csv files so that they are all the same

  10. 10

    Trying to merge different files csv and to label the columns

  11. 11

    Merge two csv files horizontally with different columns

  12. 12

    Loop through .csv files with conditions in different columns

  13. 13

    Loop through .csv files with conditions in different columns

  14. 14

    R:Extracting words from one column into different columns

  15. 15

    Extracting columns from text file with different delimiters in Linux

  16. 16

    Transfer cell values from different columns and sheets from multiple excel files with same structure into a single dataframe

  17. 17

    Merge and compare different columns from different files

  18. 18

    How to merge xts objects with slightly different columns?

  19. 19

    Two CSV files with same column names,. I need to perform linear regression comparing columns with same header name from both files

  20. 20

    Extracting same lines from two files while disregarding lower/uppercase

  21. 21

    Extracting multiple data files from a single csv file

  22. 22

    Extracting information from multiple JSON files to single CSV file in python

  23. 23

    Diff: slightly different filenames, same file

  24. 24

    Same Method for Client and Server slightly different behavior

  25. 25

    Extracting columns from a file

  26. 26

    Extracting columns from a file

  27. 27

    Compare columns from different excel files and if match occurs, append to the last column(on the same line) a status

  28. 28

    How can I add columns from 2 different files to an output in CSV python

  29. 29

    Loop in R to get specific columns and the sum of the rest from different .csv files

HotTag

Archive