Difference between revisions of "W2653 CSV File Processing"

From Coder Merlin
Line 1: Line 1:
[[File:DRAFT ICON.png|DRAFT ICON]]
== Introduction ==
== Introduction ==
This tutorial will provide an introduction about processing CSV files.
This tutorial will provide an introduction about processing CSV files.
Line 12: Line 10:


== Experiment ==
== Experiment ==
Create a new directory in your ~/projects folder, named project-2651.  Then, download this file [[Media:Example-metars.csv|Example-metars.csv]] to that directory.
Create a new directory in your ~/Experiences folder, named "W2653".  Then, download this file [[Media:Example-metars.csv|Example-metars.csv]] to that directory.
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
cd ~/projects
cd ~/Experiences
mkdir project-2651
mkdir W2563
cd project-2651
cd W2563
wget https://wiki.codermerlin.com/mediawiki/images/1/15/Example-metars.csv
wget https://codermerlin.academy/wiki/images/1/15/Example-metars.csv
</syntaxhighlight>
</syntaxhighlight>


Line 34: Line 32:
# How are '''fields''' delimited?
# How are '''fields''' delimited?


Create a new file in the current directory, "main.swift".  Perform all exercises in this lab in that file.
Create a new file in the current directory, "main.swift".  Add additional files as necessary for each class.


== Exercises ==
== Exercises ==
Line 47: Line 45:
* Think carefully about whether or not a property should be optional.
* Think carefully about whether or not a property should be optional.
* This code snippet may be helpful [[Code_Snippet:_Splitting_Key-Value_Pairs_into_Components]]
* This code snippet may be helpful [[Code_Snippet:_Splitting_Key-Value_Pairs_into_Components]]
== Key Concepts ==

Revision as of 07:28, 27 April 2023

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Introduction[edit]

This tutorial will provide an introduction about processing CSV files.

Research[edit]

Experiment[edit]

Create a new directory in your ~/Experiences folder, named "W2653". Then, download this file Example-metars.csv to that directory.

cd ~/Experiences
mkdir W2563
cd W2563
wget https://codermerlin.academy/wiki/images/1/15/Example-metars.csv

View the file in emacs:

emacs Example-metars.csv

Carefully observe the file. Remember that the "\" character as the last character in a line in emacs is a line-continuation character. (For reference, you may read Continuation-Lines.) You'll probably find it much easier to read most lines if you maximize the width of your window.

Questions:

  1. On which line does the actual data begin?
  2. How could you programmatically make this determination?
  3. What is the purpose of the immediately preceding line?
  4. How are records delimited?
  5. How are fields delimited?

Create a new file in the current directory, "main.swift". Add additional files as necessary for each class.

Exercises[edit]

  1. Design a class that will contain each field in a metar as a separate property. Pay close attention to the type of each field.
  2. Create an initializer that accepts a series of fields of the expected type
  3. Create a convenience initializer that accepts a series of strings, one for each field
  4. Create a convenience initializer that accepts a single string, in a format identical to that in the sample file
  5. Support the CustomStringConvertible protocol, providing a reasonable description of the data encapsulated in the class.

Hints: