CSV and JSON

Part of the free LogicWiz Python course, Module 8: Files. The examples and practice run in the app.

Shankar bhaiya wants to open today's sales in Excel. Excel speaks CSV. And the app's settings (GST rate, menu, opening stock) want to be saved exactly as the dict they are: that is JSON.

You'll learn to

  • Read rows with `csv.reader` and write them with `writer.writerow`/`writerows`
  • Always open CSV files with `newline=""`, and know why
  • Remember that CSV gives back strings, and convert them
  • Save and load Python data with `json.dump` and `json.load`

Learn

CSV: a table as plain text

CSV (comma-separated values) is a table saved as text: one row per line, values separated by commas, usually with a header row first.

item,qty,price
samosa,40,15
chai,65,10

You could split lines on "," yourself, but a value can itself contain a comma ("vada pav, large"), and then your split breaks. The csv module handles quotes and commas correctly, so always use it.

Reading rows

import csv

with open("sales.csv", newline="") as f:
    reader = csv.reader(f)
    header = next(reader)
    print("Columns:", header)
    for row in reader:
        item, qty, price = row
        print(item, "->", int(qty) * int(price))

Each row is a list of strings. next(reader) takes the header row so the loop gets only data. Notice "vada pav, large" stayed one value, comma and all.

Writing rows

import csv

rows = [["samosa", 40, 15], ["chai", 65, 10]]
with open("today.csv", "w", newline="") as f:
    w = csv.writer(f)
    w.writerow(["item", "qty", "price"])
    w.writerows(rows)

with open("today.csv") as f:
    print(f.read())

writerow takes one row (a list); writerows takes a list of rows. Numbers are turned into text for you, and values with commas get quoted automatically.

Always open CSV files with newline="". The csv writer ends each row with its own \r\n; without newline="", Windows turns the \n into another \r\n and you get a blank line after every row in Excel. You won't see the problem in the browser, but your laptop and your examiner will.

Totals from a CSV, by column name

import csv

total = 0
with open("sales.csv", newline="") as f:
    for row in csv.DictReader(f):
        total += int(row["qty"]) * int(row["price"])
print("Total sales: Rs", total)

csv.DictReader uses the header row as keys, so each row is a dict like {"item": "chai", "qty": "65", "price": "10"}. The values are still strings: convert before you add.

JSON: save data as it is

CSV is for tables. For nested data (a dict holding a menu dict, lists inside dicts), use JSON: text that looks almost exactly like Python's dicts and lists.

  • json.dump(data, f) writes data into an open file; json.load(f) reads it back as Python objects.
  • json.dumps(data) and json.loads(text) do the same with a string (the s is for string).

JSON keeps numbers as numbers and True/False/None as true/false/null. Two surprises: dict keys always come back as strings, and tuples come back as lists.

Saving and loading settings

import json

settings = {"gst": 5, "open": True, "menu": {"samosa": 15, "chai": 10}}
with open("settings.json", "w") as f:
    json.dump(settings, f, indent=2)

with open("settings.json") as f:
    loaded = json.load(f)
print(loaded["menu"]["chai"] + 5)
print(loaded == settings)

with open("settings.json") as f:
    print(f.read())

indent=2 makes the file readable for humans. After loading, the price is a real int again (10 + 5 = 15), unlike CSV.

CSV or JSON?

CSVJSON
A flat table: rows and columnsAny shape: nested dicts and lists
Opens in Excel and Google SheetsUsed by web APIs and app settings
csv.reader / csv.writerjson.load / json.dump
Every value comes back as a stringNumbers, bools and None come back as themselves
Daily sales, marks listsSettings, a cart, a saved game

CBSE Class 12 CSV questions: "write add() to append a record to record.csv" and "write search() to display records where...". Marks go for import csv, opening with the right mode ("a" to add, "r" to read) and newline="", creating csv.writer(f) or csv.reader(f), and using writerow. JSON is not in the CBSE syllabus, but every college course and interview expects it.

In one line

csv.reader/csv.writer (with newline="") for tables, values come back as strings; json.dump/json.load save dicts and lists as they are (keys become strings).