Reasons for using TXT, CSV and XML data sources

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

Reasons for using TXT, CSV and XML data sources

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program needs to save a simple list of player names, one per line. Which data source is the best fit?
A TXT file
B XML file
2. A school wants to store a table of students, with one record per row and the same fields in every row. Which data source suits this best?
A CSV file
B TXT file
3. Which data source is the best choice for nested, self-describing data that will be shared between two different systems?
A CSV file
B XML file
4. Which statement about saving data in a file is correct?
A The data is lost when the program closes, like a variable.
B The data is still there the next time the program runs.
5. Which reason makes CSV a good choice for a large table of records?
A Each record is one short line, so the file stays small and easy to read.
B Every value needs opening and closing tags, so the file is self-describing.

Match the Pairs

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

Left columnRight column
1scores.csv Ava,88,74 Liam,65,70 ACSV is flat, with rows and columns only, so it cannot show nested levels. XML is better because its tags can nest inside each other.
2<student><name>Ava</name></student> BTXT has no field structure, so a table of records is hard to read back. CSV is a better choice because commas separate the fields.
3TXT is the simplest choice for plain lines of text, such as a log or notes, with no fields or structure needed. Clog.txt Started game Score saved
4Data kept in a file persists between runs, so a program can save its data and load it again next time. DA CSV file suits a table of records: one record per line, with fields separated by commas, easy to open in a spreadsheet.
5Use a TXT file to store a table of 500 students with 6 fields each, because TXT is always best Ewith open('scores.csv', 'w') as f: f.write('Ava,88')
6Use CSV to store nested data, such as a school with classes that each contain students FAn XML file suits nested, self-describing data that is shared between different systems, because the tags label every value.

Definitions

TXT, CSV, XML

Fill in the Blanks

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

1.
f = open("notes.___", "r") line = f.readline()
2.
row = "Ava,7,Red" fields = row._____(",")
3.
<student><name>Ava</name></_______>
4.
tree = ET.parse("shop.___") root = tree.getroot()
5.
f = open("log.txt", "_") f.write("Saved\n")

Spot the Error

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

Question 1
A with open("todo.txt") as f:
B for line in f:
C print(line.strip)
Question 2
A import csv
B f = open("scores.csv")
C rows = csv.reader(f)
D for row in rows
E print(row[0], row[1])
Question 3
A nested = True
B shared = True
C if nested and shared:
D fmt = "CSV"
E print("Use", fmt)
Question 4
A f = open("log.txt", "r")
B f.write("Started\n")
C f.write("Saved\n")
D f.close()
Question 5
A header = "name,year,house"
B row = "Ava,7,Red"
C fields = row.split(";")
D print(fields[0])

Answer Key

Reasons for using TXT, CSV and XML data sources
Section A: Quick Questions
Q1
A — TXT file
Q2
A — CSV file
Q3
B — XML file
Q4
B — The data is still there the next time the program runs.
Q5
A — Each record is one short line, so the file stays small and easy to read.
Section B: Match the Pairs
Q1
scores.csv Ava,88,74 Liam,65,70 → A CSV file suits a table of records: one record per line, with fields separated by commas, easy to open in a spreadsheet.
Q2
<student><name>Ava</name></student> → An XML file suits nested, self-describing data that is shared between different systems, because the tags label every value.
Q3
TXT is the simplest choice for plain lines of text, such as a log or notes, with no fields or structure needed. → log.txt Started game Score saved
Q4
Data kept in a file persists between runs, so a program can save its data and load it again next time. → with open('scores.csv', 'w') as f: f.write('Ava,88')
Q5
Use a TXT file to store a table of 500 students with 6 fields each, because TXT is always best → TXT has no field structure, so a table of records is hard to read back. CSV is a better choice because commas separate the fields.
Q6
Use CSV to store nested data, such as a school with classes that each contain students → CSV is flat, with rows and columns only, so it cannot show nested levels. XML is better because its tags can nest inside each other.
Section C: Definitions
Q1
TXT, CSV, XML — Choose a data source by structure: TXT for simple lines, CSV for tables of records, XML for nested self-describing data shared between systems. All three store data between runs.
Section D: Fill in the Blanks
Q1
f = open("notes.txt", "r") line = f.readline()
Q2
row = "Ava,7,Red" fields = row.split(",")
Q3
<student><name>Ava</name></student>
Q4
tree = ET.parse("shop.xml") root = tree.getroot()
Q5
f = open("log.txt", "a") f.write("Saved\n")
Section E: Spot the Error
Q1
C — strip is a method and needs brackets to run. It should be print(line.strip()). Without them the program prints a method object instead of the text line read from the TXT file.
Q2
D — A for loop header must end with a colon. It should be for row in rows: so each CSV record is processed in turn.
Q3
D — Nested, self-describing data shared between systems suits XML. CSV only holds flat tables of records, so it should be fmt = "XML".
Q4
A — Mode "r" only reads, so the writes would fail and nothing would be stored between runs. Use "w" (or "a") to write lines to the TXT file.
Q5
C — CSV values are separated by commas, so the line must be split on ",". Splitting on ";" leaves the whole record as one field.