0Pricing
Web Scraping & Bots · Lesson

Storing Data in CSV/JSON

Learn to save scraped data into common file formats like CSV and JSON for easy portability and analysis.

Storing Data in CSV/JSON is a free Web Scraping & Bots lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Web Scraping & Bots learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Storing Your Scraped Data

After scraping, raw data is often in memory. To use it later, share it, or analyze it, you need to save it permanently.

This lesson introduces two popular file formats: CSV and JSON. They are simple, human-readable, and widely supported.

Meet CSV: Comma-Separated Values

CSV stands for Comma-Separated Values. It's a plain text file format used to store tabular data, like a spreadsheet.

Each line in a CSV file is a data record. Each record consists of one or more fields, separated by commas.

CSV: Simple Table Format

Imagine a simple table. Each row becomes a line, and each column value is separated by a comma. The first line often contains the column headers.

name,age,city
Alice,30,New York
Bob,24,London

Writing to CSV with Python

Python's built-in csv module makes writing data to CSV files easy. We'll use csv.writer and writerow().

The 'w' mode opens the file for writing. newline='' prevents extra blank rows.

import csv

# Data to save
data = [
    ["name", "age", "city"],
    ["Alice", 30, "New York"],
    ["Bob", 24, "London"]
]

# Open the file in write mode
with open("people.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerows(data) # Write all rows at once

print("Data saved to people.csv")

JSON: JavaScript Object Notation

JSON (JavaScript Object Notation) is another popular, human-readable format. It's often used for sending data between web servers and web applications.

JSON organizes data into key-value pairs, similar to Python dictionaries, and lists.

JSON: Key-Value Pairs

JSON data is structured using objects ({}) and arrays ([]).

  • An object holds key-value pairs (e.g., "name": "Alice").
  • An array holds an ordered list of values (e.g., [{}, {}]).
[
  {
    "name": "Alice",
    "age": 30,
    "city": "New York"
  },
  {
    "name": "Bob",
    "age": 24,
    "city": "London"
  }
]

Writing to JSON with Python

Python's built-in json module handles JSON data. We'll use json.dump() to write a Python dictionary or list to a file.

indent=2 makes the output file more readable by adding indentation.

import json

# Data to save (list of dictionaries)
data = [
    {"name": "Alice", "age": 30, "city": "New York"},
    {"name": "Bob", "age": 24, "city": "London"}
]

# Open the file in write mode
with open("people.json", "w") as file:
    json.dump(data, file, indent=2)

print("Data saved to people.json")

Choosing the Right Format

Both CSV and JSON are great for data storage, but they excel in different scenarios:

  • CSV: Best for simple tabular data, like spreadsheets. Easy to import into databases or Excel.
  • JSON: Ideal for complex, hierarchical data. Great for APIs and when data structure might vary.

Reading Data Back

Just as you can write data, you can also read it back! Both csv and json modules provide functions for this.

  • csv.reader() for CSV files.
  • json.load() for JSON files.

This allows you to load your scraped data back into Python for further processing.

Data Format Quiz

Which file format is generally better suited for storing hierarchical data with nested structures, like a list of products where each product has multiple attributes and possibly sub-items?

Lesson Summary

You've learned how to persist your scraped data!

  • CSV: Ideal for tabular data, easy with Python's csv module.
  • JSON: Great for hierarchical data, handled by Python's json module.
  • Choose the format that best fits your data's structure and how you plan to use it.

Saving data is crucial for analysis and future use.

Frequently asked questions

Is the “Storing Data in CSV/JSON” lesson free?

Yes — the full text of “Storing Data in CSV/JSON” is free to read here on the web, and the Web Scraping & Bots course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Web Scraping & Bots course, upgrade to CoddyKit PRO.

What will I learn in “Storing Data in CSV/JSON”?

Learn to save scraped data into common file formats like CSV and JSON for easy portability and analysis. You practise Web Scraping & Bots with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Web Scraping & Bots?

No prior experience is required. Web Scraping & Bots on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Storing Data in CSV/JSON” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Web Scraping & Bots lesson?

Yes. Every Web Scraping & Bots lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Storing Data in CSV/JSON
  2. Integrating with Databases (SQL)
  3. Cloud Storage Solutions
  4. Storing Data in NoSQL Databases
← Back to Web Scraping & Bots