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
| Word | What it means | Example |
|---|---|---|
| Data format | The way data is written in a file, so a program can read it. | CSV, JSON, XML |
| Text file | A file of plain characters. It has no fixed shape. | notes.txt |
| CSV | Comma separated values. One row per line, with a comma between the values. | Sam,16,12.50 |
| JSON | A format with curly brackets. It holds names and values, and lists. | {"name": "Sam"} |
| XML | A format with tags in angle brackets. Tags can sit inside other tags. | <student>Sam</student> |
| Tag | A label in angle brackets that starts or ends a piece of XML. | <student> |
| Attribute | An extra fact written inside the opening tag. | age="16" |
| Dictionary | In Python, a set of names with a value for each name. | {"name": "Sam"} |
| Parse | To 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
| Format | What it looks like | Best for |
|---|---|---|
| Text file | Plain words, any shape | Notes, logs, a story |
| CSV | Rows, with commas between values | Tables that open in a spreadsheet |
| JSON | Curly brackets { }, names and values | Web pages talking to servers, settings |
| XML | Tags 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
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 questionsYour 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
# 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".