Difference between revisions of "W2653 CSV File Processing"

From Coder Merlin
(Created page with "DRAFT ICON = CSV File Processing = This tutorial will provide an introduction about processing CSV files. == Research == * Read [http://www.creativys...")
 
m (Editorial review and minor corrections)
 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[File:DRAFT ICON.png|DRAFT ICON]]
== Introduction ==
 
This tutorial provides an introduction to processing CSV (comma separated values) files.
= CSV File Processing =
This tutorial will provide an introduction about processing CSV files.


== Research ==
== Research ==
* Read [http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm CSV Files (Creativyst)]
* Read [http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm CSV Files] (Creativyst)
* Read [https://en.wikipedia.org/wiki/ICAO_airport_code ICAO Airport Codes] (Wikipedia)
* Read [https://www.aviationweather.gov/info Aviation Weather Center Mission] (US Government)
* Read [https://en.wikipedia.org/wiki/METAR Metar Overview] (Wikipedia)
* Read [https://docs.swift.org/swift-book/LanguageGuide/Initialization.html#ID231 Initialization] (Swift Documentation) - Read only the section on convenience initializers


== Experiment ==
== Experiment ==
Create a new directory in your ~/Experiences folder named "W2653"; then download the file [[Media:Example-metars.csv|Example-metars.csv]] to that directory.
<syntaxhighlight lang="bash">
cd ~/Experiences
mkdir W2653
cd W2653
wget https://codermerlin.academy/wiki/images/1/15/Example-metars.csv
</syntaxhighlight>
View the file in emacs:
<syntaxhighlight lang="bash">
emacs Example-metars.csv
</syntaxhighlight>
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 can read [https://www.gnu.org/software/emacs/manual/html_node/emacs/Continuation-Lines.html Continuation-Lines].) You'll probably find it much easier to read most lines if you maximize the width of your window.
Questions:
# On which line does the actual data begin?
# How could you programmatically make this determination?
# What is the purpose of the immediately preceding line?
# How are '''records''' delimited?
# How are '''fields''' delimited?
Create a new file in the current directory, "main.swift". Remember to set up your project with "swift-init". Add additional files as necessary for each class.


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


== Key Concepts ==
Hints:
* In some cases you could opt to use more than one property for a single field in the file or vice versa.
* Think carefully about whether a property should be optional.
* This code snippet might be helpful [[Code_Snippet:_Splitting_Key-Value_Pairs_into_Components]]

Latest revision as of 16:21, 2 May 2023

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

Introduction[edit]

This tutorial provides an introduction to processing CSV (comma separated values) files.

Research[edit]

Experiment[edit]

Create a new directory in your ~/Experiences folder named "W2653"; then download the file Example-metars.csv to that directory.

cd ~/Experiences
mkdir W2653
cd W2653
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 can 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". Remember to set up your project with "swift-init". 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: