0Pricing
Web Scraping & Bots · レッスン

CSV/JSONへのデータ保存

スクレイピングしたデータをCSVやJSONなどの一般的なファイル形式で保存し、簡単に移植・分析する方法を学びます。

「CSV/JSONへのデータ保存」はCoddyKit上の無料Web Scraping & Botsレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはWeb Scraping & Bots学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Web Scraping & Botsコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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.

よくある質問

「CSV/JSONへのデータ保存」レッスンは無料ですか?

はい。「CSV/JSONへのデータ保存」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Web Scraping & Botsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Web Scraping & Botsコースには全4レッスンが含まれています。

「CSV/JSONへのデータ保存」で何を学びますか?

スクレイピングしたデータをCSVやJSONなどの一般的なファイル形式で保存し、簡単に移植・分析する方法を学びます。 ブラウザで直接実行するハンズオンコードでWeb Scraping & Botsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Web Scraping & Botsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのWeb Scraping & Botsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「CSV/JSONへのデータ保存」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このWeb Scraping & Botsレッスンでコードを書いて実行できますか?

はい。すべてのWeb Scraping & Botsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. CSV/JSONへのデータ保存
  2. データベース(SQL)との統合
  3. クラウドストレージソリューション
  4. NoSQL データベースにデータを保存する
← Web Scraping & Botsに戻る