F# How to Write/Read to a CSV File
up vote
1
down vote
favorite
I am working on an assignment using F# where I have to add in a specific student and his information to a large Students.txt file
The Student.txt file contains their lastname, firstname, middle intial, phone number, email, and their gpa
A snippet of the Students.txt file
If I am trying to add in this information and then read from the file:
type Phone =
type Email =
type StudentInfo =
firstName : string;
middleInitial : char option;
lastName : string;
phone : Phone;
email : Email option;
gpa : float
let addPhone input =
let addEmail input =
let readStudentsFromCSV filename =
let students = readStudentsFromCSV "Students.txt"
I need insight on how to write these functions.
Note: This is only a snippet of my code.
csv f#
add a comment |
up vote
1
down vote
favorite
I am working on an assignment using F# where I have to add in a specific student and his information to a large Students.txt file
The Student.txt file contains their lastname, firstname, middle intial, phone number, email, and their gpa
A snippet of the Students.txt file
If I am trying to add in this information and then read from the file:
type Phone =
type Email =
type StudentInfo =
firstName : string;
middleInitial : char option;
lastName : string;
phone : Phone;
email : Email option;
gpa : float
let addPhone input =
let addEmail input =
let readStudentsFromCSV filename =
let students = readStudentsFromCSV "Students.txt"
I need insight on how to write these functions.
Note: This is only a snippet of my code.
csv f#
Given that this a homework... Take a look at the Type Providers! They are gorgeous, just what you need. Here's the CSV Type Provider.
– psfinaki
Nov 12 at 1:01
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am working on an assignment using F# where I have to add in a specific student and his information to a large Students.txt file
The Student.txt file contains their lastname, firstname, middle intial, phone number, email, and their gpa
A snippet of the Students.txt file
If I am trying to add in this information and then read from the file:
type Phone =
type Email =
type StudentInfo =
firstName : string;
middleInitial : char option;
lastName : string;
phone : Phone;
email : Email option;
gpa : float
let addPhone input =
let addEmail input =
let readStudentsFromCSV filename =
let students = readStudentsFromCSV "Students.txt"
I need insight on how to write these functions.
Note: This is only a snippet of my code.
csv f#
I am working on an assignment using F# where I have to add in a specific student and his information to a large Students.txt file
The Student.txt file contains their lastname, firstname, middle intial, phone number, email, and their gpa
A snippet of the Students.txt file
If I am trying to add in this information and then read from the file:
type Phone =
type Email =
type StudentInfo =
firstName : string;
middleInitial : char option;
lastName : string;
phone : Phone;
email : Email option;
gpa : float
let addPhone input =
let addEmail input =
let readStudentsFromCSV filename =
let students = readStudentsFromCSV "Students.txt"
I need insight on how to write these functions.
Note: This is only a snippet of my code.
csv f#
csv f#
edited Nov 12 at 1:00
asked Nov 12 at 0:01
policy_wizard
153
153
Given that this a homework... Take a look at the Type Providers! They are gorgeous, just what you need. Here's the CSV Type Provider.
– psfinaki
Nov 12 at 1:01
add a comment |
Given that this a homework... Take a look at the Type Providers! They are gorgeous, just what you need. Here's the CSV Type Provider.
– psfinaki
Nov 12 at 1:01
Given that this a homework... Take a look at the Type Providers! They are gorgeous, just what you need. Here's the CSV Type Provider.
– psfinaki
Nov 12 at 1:01
Given that this a homework... Take a look at the Type Providers! They are gorgeous, just what you need. Here's the CSV Type Provider.
– psfinaki
Nov 12 at 1:01
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
There are a number of options. The main question is whether you want to write your own CSV parsing, or whether you want to use an existing library.
If this is an assignment, it might require you to write your own parser (doing that would probably be a bad idea in the real world, because real world CSV files can be very messy, but it might be fine if your input is very regular). If you were to use a library, the F# Data library is what most people in the F# community would use.
F# Data comes with a type provider called
CsvProvider
which infers the type of rows in a CSV file for you, so you do not have to write explicit type definitions. (Or you can still do that, but then load data into your structures using a simple transformation.)F# Data also has
CsvFile
type, which just does the parsing, but then returns data as a sequence of rows (which are themselves arrays of string values). This might be nice if you just need something to take care of splitting the lines, but want to do the rest of the work.If you wanted to write CSV parsing on your own, you can use
File.ReadAllLines
to read individual rows and thenrow.Split(',')
to turn each row into an array of strings using,
as the separator. This could work on your file - but it will break if there is any escaping in your file (for exampleFoo, "a, b", Bar
is just three columns!
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254469%2ff-how-to-write-read-to-a-csv-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
There are a number of options. The main question is whether you want to write your own CSV parsing, or whether you want to use an existing library.
If this is an assignment, it might require you to write your own parser (doing that would probably be a bad idea in the real world, because real world CSV files can be very messy, but it might be fine if your input is very regular). If you were to use a library, the F# Data library is what most people in the F# community would use.
F# Data comes with a type provider called
CsvProvider
which infers the type of rows in a CSV file for you, so you do not have to write explicit type definitions. (Or you can still do that, but then load data into your structures using a simple transformation.)F# Data also has
CsvFile
type, which just does the parsing, but then returns data as a sequence of rows (which are themselves arrays of string values). This might be nice if you just need something to take care of splitting the lines, but want to do the rest of the work.If you wanted to write CSV parsing on your own, you can use
File.ReadAllLines
to read individual rows and thenrow.Split(',')
to turn each row into an array of strings using,
as the separator. This could work on your file - but it will break if there is any escaping in your file (for exampleFoo, "a, b", Bar
is just three columns!
add a comment |
up vote
2
down vote
accepted
There are a number of options. The main question is whether you want to write your own CSV parsing, or whether you want to use an existing library.
If this is an assignment, it might require you to write your own parser (doing that would probably be a bad idea in the real world, because real world CSV files can be very messy, but it might be fine if your input is very regular). If you were to use a library, the F# Data library is what most people in the F# community would use.
F# Data comes with a type provider called
CsvProvider
which infers the type of rows in a CSV file for you, so you do not have to write explicit type definitions. (Or you can still do that, but then load data into your structures using a simple transformation.)F# Data also has
CsvFile
type, which just does the parsing, but then returns data as a sequence of rows (which are themselves arrays of string values). This might be nice if you just need something to take care of splitting the lines, but want to do the rest of the work.If you wanted to write CSV parsing on your own, you can use
File.ReadAllLines
to read individual rows and thenrow.Split(',')
to turn each row into an array of strings using,
as the separator. This could work on your file - but it will break if there is any escaping in your file (for exampleFoo, "a, b", Bar
is just three columns!
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
There are a number of options. The main question is whether you want to write your own CSV parsing, or whether you want to use an existing library.
If this is an assignment, it might require you to write your own parser (doing that would probably be a bad idea in the real world, because real world CSV files can be very messy, but it might be fine if your input is very regular). If you were to use a library, the F# Data library is what most people in the F# community would use.
F# Data comes with a type provider called
CsvProvider
which infers the type of rows in a CSV file for you, so you do not have to write explicit type definitions. (Or you can still do that, but then load data into your structures using a simple transformation.)F# Data also has
CsvFile
type, which just does the parsing, but then returns data as a sequence of rows (which are themselves arrays of string values). This might be nice if you just need something to take care of splitting the lines, but want to do the rest of the work.If you wanted to write CSV parsing on your own, you can use
File.ReadAllLines
to read individual rows and thenrow.Split(',')
to turn each row into an array of strings using,
as the separator. This could work on your file - but it will break if there is any escaping in your file (for exampleFoo, "a, b", Bar
is just three columns!
There are a number of options. The main question is whether you want to write your own CSV parsing, or whether you want to use an existing library.
If this is an assignment, it might require you to write your own parser (doing that would probably be a bad idea in the real world, because real world CSV files can be very messy, but it might be fine if your input is very regular). If you were to use a library, the F# Data library is what most people in the F# community would use.
F# Data comes with a type provider called
CsvProvider
which infers the type of rows in a CSV file for you, so you do not have to write explicit type definitions. (Or you can still do that, but then load data into your structures using a simple transformation.)F# Data also has
CsvFile
type, which just does the parsing, but then returns data as a sequence of rows (which are themselves arrays of string values). This might be nice if you just need something to take care of splitting the lines, but want to do the rest of the work.If you wanted to write CSV parsing on your own, you can use
File.ReadAllLines
to read individual rows and thenrow.Split(',')
to turn each row into an array of strings using,
as the separator. This could work on your file - but it will break if there is any escaping in your file (for exampleFoo, "a, b", Bar
is just three columns!
answered Nov 12 at 11:39
Tomas Petricek
197k13286460
197k13286460
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254469%2ff-how-to-write-read-to-a-csv-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Given that this a homework... Take a look at the Type Providers! They are gorgeous, just what you need. Here's the CSV Type Provider.
– psfinaki
Nov 12 at 1:01