CSV/JSON으로 데이터 저장
쉽게 이식하고 분석할 수 있도록 스크래핑한 데이터를 CSV 및 JSON과 같은 일반적인 파일 형식으로 저장하는 방법을 학습합니다.
CSV/JSON으로 데이터 저장은(는) CoddyKit의 무료 Web Scraping & Bots 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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,LondonWriting 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
csvmodule. - JSON: Great for hierarchical data, handled by Python's
jsonmodule. - 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/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Web Scraping & Bots 강의 전체를 잠금 해제할 수 있습니다. Web Scraping & Bots 강의에는 총 4개의 강의가 포함되어 있습니다.
“CSV/JSON으로 데이터 저장”에서 뭘 배우나요?
쉽게 이식하고 분석할 수 있도록 스크래핑한 데이터를 CSV 및 JSON과 같은 일반적인 파일 형식으로 저장하는 방법을 학습합니다. 브라우저에서 직접 실행하는 실습 코드로 Web Scraping & Bots을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Web Scraping & Bots을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Web Scraping & Bots은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.
“CSV/JSON으로 데이터 저장” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Web Scraping & Bots 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Web Scraping & Bots 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- CSV/JSON으로 데이터 저장
- 데이터베이스(SQL) 통합
- 클라우드 저장소 솔루션
- NoSQL 데이터베이스에 데이터 저장하기