Module 3 of 6 · 30 minutes

Files, text and JSON

AI work is full of text and JSON. This module shows you how to read, write, clean and safely parse them.

Learning objectives

  • Read and write text files with encoding
  • Convert between JSON and Python with json.loads and json.dumps
  • Handle bad input with try/except
  • Clean and search text with string methods and regular expressions
  • Read and write CSV files

3.1 Reading and writing text files

with open("notes.txt", "w", encoding="utf-8") as f:
    f.write("First line\nSecond line\n")

with open("notes.txt", encoding="utf-8") as f:
    for line in f:
        print(line.strip())

The with block closes the file automatically. Always set encoding="utf-8" for text that might include accents or emojis.

3.2 JSON: the language of AI APIs

AI model APIs send requests and responses as JSON. Python's json module converts between JSON text and dictionaries and lists.

import json

raw = '{"category": "DELIVERY", "urgent": true, "summary": "Parcel late"}'
data = json.loads(raw)            # JSON text -> Python dict
print(data["category"], data["urgent"])     # DELIVERY True

reply = {"order": "GFY-10422", "items": ["board", "oil"], "total": 31.50}
text = json.dumps(reply, indent=2)           # Python -> JSON text
with open("reply.json", "w") as f:
    f.write(text)

Note how JSON true becomes Python True, and null becomes None.

JSON text (from an API){"category": "DELIVERY", "urgent": true, "summary": null}Python dict{'category': 'DELIVERY', 'urgent': True, 'summary': None}json.loadsjson.dumps
json.loads turns JSON text into Python objects; json.dumps does the reverse. Note true → True and null → None.

3.3 Handling bad data with try/except

AI models sometimes return text that isn't valid JSON. Handle it rather than crashing:

def parse_json(text):
    try:
        return json.loads(text)
    except json.JSONDecodeError:
        return None

print(parse_json('{"ok": true}'))        # {'ok': True}
print(parse_json("Sure! Here is JSON:"))  # None

3.4 Cleaning text

import re

def clean(text):
    text = text.strip().lower()
    text = re.sub(r"\s+", " ", text)          # collapse repeated spaces/newlines
    text = re.sub(r"[^\w\s£.,!?'-]", "", text)  # drop unusual symbols
    return text

print(clean("  GREAT   board!!! 😀\n\nArrived    late  "))

re is Python's regular expressions module — handy for finding patterns such as order numbers or email addresses.

3.5 CSV files

import csv

rows = [{"id": 1, "text": "Love it", "stars": 5}, {"id": 2, "text": "Too small", "stars": 2}]
with open("reviews.csv", "w", newline="", encoding="utf-8") as f:
    writer = csv.DictWriter(f, fieldnames=["id", "text", "stars"])
    writer.writeheader()
    writer.writerows(rows)

with open("reviews.csv", encoding="utf-8") as f:
    for row in csv.DictReader(f):
        print(row["text"], row["stars"])

For anything beyond simple CSVs, you'll use pandas (Module 4).

Exercise: extract order numbers

Write a function that finds every order number in the format GFY-##### (five digits) in a piece of text and returns them as a list.

Show a solution
import re

def find_orders(text):
    return re.findall(r"GFY-\d{5}", text)

print(find_orders("Orders GFY-10422 and GFY-10423 are late; GFY-99 is invalid"))
# ['GFY-10422', 'GFY-10423']

Key takeaways

  • Use with open(..., encoding='utf-8') for files.
  • json.loads parses JSON text; json.dumps creates it.
  • Wrap parsing in try/except — AI output isn't always valid JSON.
  • re finds and cleans patterns in text.
  • csv handles simple tables; pandas handles the rest.