Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

Text, CSV, JSON and XML

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

50 minutes

One list, four ways to write it

You have a list of three students on a school trip. You must send it to another program.

You can write it as plain words. You can write it with commas. You can write it with curly brackets. Or you can write it with tags.

All four hold the same facts. But each one suits a different job. We look at them now.

Key words

WordWhat it meansExample
Data formatThe way data is written in a file, so a program can read it.CSV, JSON, XML
Text fileA file of plain characters. It has no fixed shape.notes.txt
CSVComma separated values. One row per line, with a comma between the values.Sam,16,12.50
JSONA format with curly brackets. It holds names and values, and lists.{"name": "Sam"}
XMLA format with tags in angle brackets. Tags can sit inside other tags.<student>Sam</student>
TagA label in angle brackets that starts or ends a piece of XML.<student>
AttributeAn extra fact written inside the opening tag.age="16"
DictionaryIn Python, a set of names with a value for each name.{"name": "Sam"}
ParseTo read text in a format and turn it into data your program can use.Split a CSV line into its values

The cells students fill in are left blank in this preview.

The four formats

FormatWhat it looks likeBest for
Text filePlain words, any shapeNotes, logs, a story
CSVRows, with commas between valuesTables that open in a spreadsheet
JSONCurly brackets { }, names and valuesWeb pages talking to servers, settings
XMLTags inside tags, in angle brackets < >Documents with parts inside parts, and older systems

The cells students fill in are left blank in this preview.

Try it: CSV

Code: python
text = "name,age,paid\nSam,16,12.50\nAisha,17,5.00"
print(text)

lines = text.split("\n")
lines.pop(0)     # take off the headings line
for line in lines:
    values = line.split(",")
    print(values[0], "paid", values[2])

# Everything from a CSV is a string (text). Try float(values[2]).

Quick check: which format?

Have a go

Quick check: which format?

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

JSON in plain words

JSON holds names and values. A name is in quote marks. A colon sits between the name and the value.

A value can be text or a number. It can be true or false. It can be a list in square brackets. A web page and a server often use JSON to talk.

Try it: JSON

Code: python
# JSON looks like a Python dictionary.
text = '{"trip": "Zoo", "price": 12.5, "students": ["Sam", "Aisha"], "coach": true}'
print(text)

# JSON writes true. Python writes True.
data = {"trip": "Zoo", "price": 12.5, "students": ["Sam", "Aisha"], "coach": True}
print(data["trip"])
print(data["students"])
print(len(data["students"]))

JSON is strict

data-callouts.warning.label

JSON needs double quote marks ("like this"), not single ones. It does not allow a comma after the last item. One small mistake and the whole file will not parse.

XML in plain words

XML puts every piece of data between an opening tag and a closing tag, such as <student>Sam</student>.

A tag can sit inside another tag. The top tag is called the root. Extra facts go in attributes, inside the opening tag, such as age="16".

The lesson goes on

9 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.