Block 41: CSV & JSON with Standard Library
Read/write CSV and JSON using Python's built-in modules.
Concepts
- csv.reader and csv.DictReader
- csv.writer and csv.DictWriter
- json.load(), json.dump(), json.loads(), json.dumps()
- Handling encoding and special characters
Code Examples
See exercise below.
Exercise
Write a list of student dicts to CSV and read it back with csv.DictReader. Save a dict as pretty-printed JSON and reload it.
Homework
When would you use the csv module vs pandas read_csv? Give 2 scenarios for each.