HomeCourseModule 07 › Text files, JSON, and other formats

Text files, JSON, and other formats

Module 07 · Working with Files7 min readBeginner

What you'll learn

  • Read and write JSON
  • Parse log-style text files
  • Know when to reach for a different format

JSON in two lines

JSON is the format APIs and lots of modern tools use. Python's json module reads it straight into a dict.

import json

# Read
with open("config.json") as f:
    config = json.load(f)
print(config["api_key"])

# Write
data = {"name": "Alice", "scores": [10, 20, 30]}
with open("out.json", "w") as f:
    json.dump(data, f, indent=2)

JSON ↔ DataFrame

import pandas as pd
df = pd.read_json("orders.json")                  # array of objects
df.to_json("clean.json", orient="records", indent=2)

Parsing a log file

lines = Path("server.log").read_text().splitlines()
errors = [l for l in lines if "ERROR" in l]
print(len(errors), "errors")

XML — yes, you'll occasionally see it

For old enterprise systems and government data, you'll get XML. The fastest way: pandas can read simple XML tables.

df = pd.read_xml("data.xml")

For gnarlier XML, the standard library's xml.etree.ElementTree works, and the third-party lxml is faster.

Key takeaways

  • JSON: json.load / json.dump — clean dict in, clean dict out.
  • Plain text: Path(...).read_text().splitlines() is the one-liner.
  • pandas can also read JSON and XML directly into DataFrames.

Config saver

Write a script that saves your settings (any three key/value pairs you like) to a JSON file, then reads them back and prints them.

📹 Video walkthrough
A video walkthrough of this lesson will be embedded here. Until then, the written walkthrough above mirrors what the video will cover step-for-step.