Delimited (CSV) files: structure

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Delimited (CSV) files: structure

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. In a CSV file, how is each record stored?
A One record per line
B One record per field
2. Which line is a valid CSV record with three fields?
A Ava 16 Melbourne,
B Ava,16,Melbourne
3. What is the job of the delimiter character in a CSV file?
A It separates one field from the next
B It marks the end of the whole file
4. What does the optional header row contain?
A The total number of records
B The names of the fields
5. A CSV file has a header row plus 5 lines of data. How many records does it store?
A 5 records
B 6 records

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1name,year,house AOne record on a single line, with three fields separated by commas
2Ava,7,Red BAva;7;Red
3Text that contains a comma is wrapped in double quotes so the comma is not treated as a delimiter CA header row: the first line lists the field names, separated by commas
4A delimiter other than a comma, such as a semicolon, can separate the fields in a delimited file D"Nguyen, Ava",7,Red
5Nguyen, Ava,7,Red EThe comma inside the name is not quoted, so it is read as a delimiter and the line splits into four fields instead of three. Fix: "Nguyen, Ava",7,Red
6name,year,house Ava,7,Red FThe header and the record are on the same line. Each record must be on its own line, so the header and the record need a line break between them.

Definitions

CSV

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
fields = line._____(",")
2.
delimiter = ";" fields = line.split(_________)
3.
header = lines[_] records = lines[1:]
4.
recordCount = len(lines) _ 1
5.
line = ",".____(fields)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A name,age,house
B Ava,15,Red
C Liam,16,,Blue
Question 2
A id,name,score
B 1;Ava;90
C 2,Liam,85
Question 3
A item,price,note
B Pen,2.5,"blue, fine tip"
C Book,12,red, hardcover
Question 4
A name,age,year
B Zoe,14,9
C Leo,15
D Mia,13,8
Question 5
A name,age
B Ava,15,Perth
C Liam,16,Hobart

Answer Key

Delimited (CSV) files: structure
Section A: Quick Questions
Q1
A — One record per line
Q2
B — Ava,16,Melbourne
Q3
A — It separates one field from the next
Q4
B — The names of the fields
Q5
A — 5 records
Section B: Match the Pairs
Q1
name,year,house → A header row: the first line lists the field names, separated by commas
Q2
Ava,7,Red → One record on a single line, with three fields separated by commas
Q3
Text that contains a comma is wrapped in double quotes so the comma is not treated as a delimiter → "Nguyen, Ava",7,Red
Q4
A delimiter other than a comma, such as a semicolon, can separate the fields in a delimited file → Ava;7;Red
Q5
Nguyen, Ava,7,Red → The comma inside the name is not quoted, so it is read as a delimiter and the line splits into four fields instead of three. Fix: "Nguyen, Ava",7,Red
Q6
name,year,house Ava,7,Red → The header and the record are on the same line. Each record must be on its own line, so the header and the record need a line break between them.
Section C: Definitions
Q1
CSV — A text file with one record per line, fields split by a delimiter such as a comma, and an optional header line of field names. Text containing commas goes in quotes.
Section D: Fill in the Blanks
Q1
fields = line.split(",")
Q2
delimiter = ";" fields = line.split(delimiter)
Q3
header = lines[0] records = lines[1:]
Q4
recordCount = len(lines) - 1
Q5
line = ",".join(fields)
Section E: Spot the Error
Q1
C — The double comma creates an extra empty field, so this record has 4 fields while the header has 3. It should be Liam,16,Blue.
Q2
B — The header uses commas as the delimiter, so every record must too. Semicolons would make the whole line read as one field. It should be 1,Ava,90.
Q3
C — The note contains a comma, so it must be wrapped in double quotes. Without them the line splits into 4 fields. It should be Book,12,"red, hardcover".
Q4
C — The header has 3 fields, so every record needs 3 fields. This record is missing the year. It should be Leo,15,10.
Q5
A — The header row must list a name for every field in the records. The records have 3 fields but the header names only 2. It should be name,age,city.