Python
Serialization: pickle, JSON & CSV
31 flashcards · answers and spaced-repetition review in the KnowCard app
What is the difference between pickle.dump / load and pickle.dumps / loads?
A web server unpickles a session blob that arrived from the client. A security reviewer flags it immediately. Why?
You pickle an object to a file, then a colleague tries to load it and gets an UnpicklingError. The data file is intact. What's the likely cause?
Which serializer should you reach for: arbitrary Python objects (custom classes, sets, tuples) to a local cache file, same Python program reads it back?
What's wrong with this pickle code?
```
with open("data.pkl", "w") as f:
pickle.dump(obj, f)
```
You dump three objects to one file with three pickle.dump calls. How do you read all three back?
What does this print?
```
import json
s = json.dumps({1: "a", 2: "b"})
d = json.loads(s)
print(list(d.keys()))
```
What does this do?
```
import json
json.dumps({"tags": {"a", "b"}})
```
What does this print?
```
import json
t = (1, 2, 3)
print(json.loads(json.dumps(t)))
```
Which module sends a config to a non-Python service over the network — and why not the other one?
How do you write JSON to a file so it's human-readable and the keys are ordered?
Does opening a file in binary mode differ between json.dump and pickle.dump?
What does this print?
```
import csv, io
rows = csv.reader(io.StringIO("3,4\n"))
row = next(rows)
print(row[0] + row[1])
```
On Windows you write CSV with csv.writer and the output file has a blank line between every data row. What did you forget?
What does a csv.reader yield for each line of a CSV file?
When does csv.DictReader give you dicts instead of csv.reader's lists — and where do the keys come from?
What's the bug?
```
w = csv.DictWriter(f, ["a", "b"])
w.writerows(data) # data = dicts
```
Your CSV uses semicolons, not commas. How do you read it correctly?
Why prefer csv.reader / csv.writer over splitting lines on "," yourself?
What does csv's QUOTE_NONNUMERIC do differently on read vs write?
You need to persist a lambda and an open socket with pickle. Which of your objects can pickle actually serialize, and which can't?
If you call pickle.dumps(obj) with no protocol argument, which protocol version is used — and how many exist?
You're dumping hundreds of objects into one file and it's tedious to pass the file and protocol on every pickle.dump call. What does pickle offer instead?
After a JSON round-trip, what Python type do you get back for a JSON number like 12, a number like 12.34, true, and null?
Which dialects does the csv module ship with out of the box, and how do you list them?
You want a reusable named CSV format (tab delimiter, quote everything). Why shouldn't you instantiate csv.Dialect yourself, and what do you use?
A CSV field contains a literal double-quote character. How does the csv module encode it, and what changes if doublequote=False?
You set lineterminator="|" on a csv dialect but your reader still splits on plain newlines. Bug or expected?
What are the csv module's four quoting modes, and which is the default?
Your CSV has a space after each comma ("Bob, 42") and you get values like " 42" with a leading space. Which csv option fixes it?
You don't know a CSV file's delimiter ahead of time. How can csv guess it, and why treat the result cautiously?
Start learning today
Free to start — download the app or use it in your browser.
