0Pricing
Web Scraping & Bots · บทเรียน

การจัดเก็บข้อมูลใน CSV/JSON

เรียนรู้การบันทึกข้อมูลที่ดึงมาในรูปแบบไฟล์ทั่วไป เช่น CSV และ JSON เพื่อให้เคลื่อนย้ายและวิเคราะห์ได้ง่าย

การจัดเก็บข้อมูลใน CSV/JSON เป็นบทเรียน Web Scraping & Bots ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web Scraping & Bots ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web Scraping & Bots มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจัดเก็บข้อมูลใน CSV/JSON”

เรียนรู้การบันทึกข้อมูลที่ดึงมาในรูปแบบไฟล์ทั่วไป เช่น CSV และ JSON เพื่อให้เคลื่อนย้ายและวิเคราะห์ได้ง่าย คุณปฏิบัติ Web Scraping & Bots ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web Scraping & Bots หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web Scraping & Bots บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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