0Pricing
Web Scraping & Bots · Lekcja

Przechowywanie danych w formatach CSV/JSON

Nauczą się Państwo zapisywać zescrapowane dane w popularnych formatach plików, takich jak CSV i JSON, aby ułatwić ich przenoszenie i analizę.

Przechowywanie danych w formatach CSV/JSON to bezpłatna lekcja Web Scraping & Bots na CoddyKit. To lekcja 1 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Web Scraping & Bots, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Web Scraping & Bots zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.

Często zadawane pytania

Czy lekcja „Przechowywanie danych w formatach CSV/JSON” jest bezpłatna?

Tak — pełny tekst „Przechowywanie danych w formatach CSV/JSON” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Web Scraping & Bots, przejdź na CoddyKit PRO. Kurs Web Scraping & Bots zawiera 4 lekcji w sumie.

Co nauczysz się w „Przechowywanie danych w formatach CSV/JSON”?

Nauczą się Państwo zapisywać zescrapowane dane w popularnych formatach plików, takich jak CSV i JSON, aby ułatwić ich przenoszenie i analizę. Ćwiczysz Web Scraping & Bots z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Web Scraping & Bots?

Nie wymagamy żadnego doświadczenia. Web Scraping & Bots w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 1 z 4.

Ile czasu zajmuje lekcja „Przechowywanie danych w formatach CSV/JSON”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Web Scraping & Bots?

Tak. Każda lekcja Web Scraping & Bots zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Przechowywanie danych w formatach CSV/JSON
  2. Integracja z bazami danych (SQL)
  3. Rozwiązania do przechowywania danych w chmurze
  4. Przechowywanie danych w bazach NoSQL
← Powrót do Web Scraping & Bots