Skip to contentSkip to navigation

File Handling and Data Formats

Files & Data Formats — cheat sheet

Read, write, and process modern file formats with pathlib, CSV, and JSON.

Modern pathlib

p = Path("data") / "file.txt"
Cross-platform path composition.
text = p.read_text(encoding="utf-8")
Read entire file into string.

JSON & CSV

data = json.loads(raw_json_str)
Parse JSON string to Python dict.
reader = csv.DictReader(f)
Iterate CSV lines as dictionaries.
All cheat sheets