§ 2.8section 16 of 18·~5 min
Reading and writing files
Data engineers move files all day: CSV exports from vendors, JSON from APIs. Open files with with open(...) so they're closed automatically, even if something fails.
import csv
with open("orders.csv") as f:
reader = csv.DictReader(f) # each row becomes a dict
rows = list(reader)
print(rows[0])
OUTPUT
{'id': '1042', 'amount': '499', 'city': 'Pune'}Every value comes back as a string. Convert types (section 1.2.2) before doing maths.
import json
with open("orders.json", "w") as f:
json.dump(rows, f, indent=2)
with open("orders.json") as f:
data = json.load(f)
| Mode | Meaning |
|---|---|
"r" | Read (the default) |
"w" | Write, replacing what was there |
"a" | Append to the end |