Zum Hauptinhalt springen

JSON๊ณผ CSV

In Python JSON๊ณผ CSV ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” Methode lernen wir. ์ด ๋‘ ๊ฐ€์ง€๋Š” ๊ฐ€์žฅ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” ๋ฐ์ดํ„ฐ ๊ตํ™˜ ํ˜•์‹.

JSON ๋‹ค๋ฃจ๊ธฐ ๐Ÿ“ฆโ€‹

JSON(JavaScript Object Notation)์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์ „์†กํ•˜๋Š” ๊ฒฝ๋Ÿ‰ ํ˜•์‹.

Grundlegend ์‚ฌ์šฉ๋ฒ•โ€‹

import json

# Python ๊ฐ์ฒด โ†’ JSON ๋ฌธ์ž์—ด
data = {
'name': 'ํ™๊ธธ๋™',
'age': 30,
'city': '์„œ์šธ',
'hobbies': ['๋…์„œ', '๋“ฑ์‚ฐ', '์š”๋ฆฌ']
}

json_string = json.dumps(data)
print(json_string)
# {"name": "ํ™๊ธธ๋™", "age": 30, "city": "์„œ์šธ", "hobbies": ["๋…์„œ", "๋“ฑ์‚ฐ", "์š”๋ฆฌ"]}

# JSON ๋ฌธ์ž์—ด โ†’ Python ๊ฐ์ฒด
parsed_data = json.loads(json_string)
print(parsed_data['name']) # ํ™๊ธธ๋™

ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๊ณ  ์ฝ๊ธฐโ€‹

import json

# JSON ํŒŒ์ผ๋กœ ์ €์žฅ
data = {
'users': [
{'id': 1, 'name': '๊น€์ฒ ์ˆ˜', 'email': 'kim@example.com'},
{'id': 2, 'name': '์ด์˜ํฌ', 'email': 'lee@example.com'},
]
}

with open('users.json', 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=2)

# JSON Dateien lesen
with open('users.json', 'r', encoding='utf-8') as f:
loaded_data = json.load(f)
print(loaded_data)

JSON ํฌ๋งทํŒ… ์˜ต์…˜โ€‹

import json

data = {'name': 'ํ™๊ธธ๋™', 'age': 30, 'skills': ['Python', 'JavaScript']}

# ๋“ค์—ฌ์“ฐ๊ธฐ ์—†์Œ (์••์ถ•)
compact = json.dumps(data)
print(compact)
# {"name": "ํ™๊ธธ๋™", "age": 30, "skills": ["Python", "JavaScript"]}

# ๋“ค์—ฌ์“ฐ๊ธฐ 2์นธ (์ฝ๊ธฐ ์‰ฝ๊ฒŒ)
pretty = json.dumps(data, indent=2, ensure_ascii=False)
print(pretty)
# {
# "name": "ํ™๊ธธ๋™",
# "age": 30,
# "skills": [
# "Python",
# "JavaScript"
# ]
# }

# ํ‚ค ์ •๋ ฌ
sorted_json = json.dumps(data, sort_keys=True, indent=2, ensure_ascii=False)
print(sorted_json)

๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž… ์ฒ˜๋ฆฌโ€‹

import json
from datetime import datetime
from decimal import Decimal

class DateTimeEncoder(json.JSONEncoder):
"""datetime์„ JSON์œผ๋กœ ์ง๋ ฌํ™”"""
def default(self, obj):
if isinstance(obj, datetime):
return obj.isoformat()
if isinstance(obj, Decimal):
return float(obj)
return super().default(obj)

# ์‚ฌ์šฉ Beispiel
data = {
'timestamp': datetime.now(),
'amount': Decimal('123.45')
}

json_string = json.dumps(data, cls=DateTimeEncoder, indent=2)
print(json_string)

CSV ๋‹ค๋ฃจ๊ธฐ ๐Ÿ“Šโ€‹

CSV(Comma-Separated Values)๋Š” ํ‘œ ํ˜•์‹ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฐ„๋‹จํ•œ ํ˜•์‹.

Grundlegend CSV ์“ฐ๊ธฐโ€‹

import csv

# CSV Dateien schreiben
data = [
['์ด๋ฆ„', '๋‚˜์ด', '๋„์‹œ'],
['ํ™๊ธธ๋™', 30, '์„œ์šธ'],
['๊น€์ฒ ์ˆ˜', 25, '๋ถ€์‚ฐ'],
['์ด์˜ํฌ', 28, '๋Œ€๊ตฌ']
]

with open('users.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerows(data)

CSV ์ฝ๊ธฐโ€‹

import csv

# CSV Dateien lesen
with open('users.csv', 'r', encoding='utf-8') as f:
reader = csv.reader(f)
for row in reader:
print(row)
# ['์ด๋ฆ„', '๋‚˜์ด', '๋„์‹œ']
# ['ํ™๊ธธ๋™', '30', '์„œ์šธ']
# ...

DictReader: ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ฝ๊ธฐโ€‹

import csv

# ํ—ค๋”๋ฅผ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ๊ธฐ
with open('users.csv', 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
for row in reader:
print(f"{row['์ด๋ฆ„']}์€ {row['๋‚˜์ด']}์„ธ์ด๊ณ  {row['๋„์‹œ']}์— ์‚ฝ๋‹ˆ๋‹ค.")
# ํ™๊ธธ๋™์€ 30์„ธ์ด๊ณ  ์„œ์šธ์— ์‚ฝ๋‹ˆ๋‹ค.
# ๊น€์ฒ ์ˆ˜๋Š” 25์„ธ์ด๊ณ  ๋ถ€์‚ฐ์— ์‚ฝ๋‹ˆ๋‹ค.

DictWriter: ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์“ฐ๊ธฐโ€‹

import csv

# ๋”•์…”๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ CSV๋กœ ์ €์žฅ
users = [
{'name': 'ํ™๊ธธ๋™', 'age': 30, 'city': '์„œ์šธ'},
{'name': '๊น€์ฒ ์ˆ˜', 'age': 25, 'city': '๋ถ€์‚ฐ'},
{'name': '์ด์˜ํฌ', 'age': 28, 'city': '๋Œ€๊ตฌ'}
]

with open('users_dict.csv', 'w', newline='', encoding='utf-8') as f:
fieldnames = ['name', 'age', 'city']
writer = csv.DictWriter(f, fieldnames=fieldnames)

writer.writeheader() # ํ—ค๋” ์“ฐ๊ธฐ
writer.writerows(users) # ๋ชจ๋“  ํ–‰ ์“ฐ๊ธฐ

CSV ์˜ต์…˜โ€‹

import csv

# ๊ตฌ๋ถ„์ž ๋ณ€๊ฒฝ (ํƒญ, ์„ธ๋ฏธ์ฝœ๋ก  ๋“ฑ)
with open('data.tsv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f, delimiter='\t')
writer.writerow(['์ด๋ฆ„', '๋‚˜์ด'])
writer.writerow(['ํ™๊ธธ๋™', 30])

# ์ธ์šฉ ์Šคํƒ€์ผ
with open('quoted.csv', 'w', newline='', encoding='utf-8') as f:
# QUOTE_ALL: ๋ชจ๋“  ํ•„๋“œ๋ฅผ ์ธ์šฉ
writer = csv.writer(f, quoting=csv.QUOTE_ALL)
writer.writerow(['์ด๋ฆ„', '๋‚˜์ด'])

# QUOTE_MINIMAL: ํ•„์š”ํ•œ ๊ฒฝ์šฐ๋งŒ ์ธ์šฉ (Grundlegend๊ฐ’)
writer = csv.writer(f, quoting=csv.QUOTE_MINIMAL)
writer.writerow(['ํ™๊ธธ๋™', 30])

์‹ค์ „ Beispiel ๐Ÿ’กโ€‹

Beispiel 1: API ์‘๋‹ต ํŒŒ์‹ฑโ€‹

import json
import urllib.request

def fetch_and_parse_json(url):
"""JSON API ํ˜ธ์ถœ ๋ฐ ํŒŒ์‹ฑ"""
try:
with urllib.request.urlopen(url) as response:
data = response.read()
return json.loads(data)
except Exception as e:
print(f'์—๋Ÿฌ ๋ฐœ์ƒ: {e}')
return None

# ์‚ฌ์šฉ Beispiel (์‹ค์ œ API ์‚ฌ์šฉ)
# url = 'https://api.example.com/users'
# users = fetch_and_parse_json(url)

# ๋กœ์ปฌ JSON Dateien lesen Beispiel
def load_user_data(filename):
"""์‚ฌ์šฉ์ž ๋ฐ์ดํ„ฐ ๋กœ๋“œ"""
try:
with open(filename, 'r', encoding='utf-8') as f:
return json.load(f)
except FileNotFoundError:
print(f'ํŒŒ์ผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค: {filename}')
return []
except json.JSONDecodeError as e:
print(f'JSON ํŒŒ์‹ฑ ์—๋Ÿฌ: {e}')
return []

# users = load_user_data('users.json')
# for user in users:
# print(f"{user['name']}: {user['email']}")

Beispiel 2: ์„ค์ • ํŒŒ์ผ ๊ด€๋ฆฌโ€‹

import json
from pathlib import Path

class Config:
"""JSON ์„ค์ • ํŒŒ์ผ ๊ด€๋ฆฌ์ž"""

def __init__(self, config_file='config.json'):
self.config_file = Path(config_file)
self.data = self.load()

def load(self):
"""์„ค์ • ํŒŒ์ผ ๋กœ๋“œ"""
if self.config_file.exists():
with open(self.config_file, 'r', encoding='utf-8') as f:
return json.load(f)
return self.get_default()

def save(self):
"""์„ค์ • ํŒŒ์ผ ์ €์žฅ"""
with open(self.config_file, 'w', encoding='utf-8') as f:
json.dump(self.data, f, indent=2, ensure_ascii=False)

def get(self, key, default=None):
"""์„ค์ • ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ"""
return self.data.get(key, default)

def set(self, key, value):
"""์„ค์ • ๊ฐ’ ์„ค์ •ํ•˜๊ธฐ"""
self.data[key] = value
self.save()

def get_default(self):
"""Grundlegend ์„ค์ •"""
return {
'app_name': 'MyApp',
'version': '1.0.0',
'debug': False,
'max_connections': 100
}

# ์‚ฌ์šฉ Beispiel
config = Config()
print(f"์•ฑ ์ด๋ฆ„: {config.get('app_name')}")

config.set('debug', True)
print(f"๋””๋ฒ„๊ทธ ๋ชจ๋“œ: {config.get('debug')}")

Beispiel 3: CSV์—์„œ JSON์œผ๋กœ ๋ณ€ํ™˜โ€‹

import csv
import json

def csv_to_json(csv_file, json_file):
"""CSV ํŒŒ์ผ์„ JSON์œผ๋กœ ๋ณ€ํ™˜"""
data = []

with open(csv_file, 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
for row in reader:
# ์ˆซ์ž ํƒ€์ž… ๋ณ€ํ™˜
for key, value in row.items():
if value.isdigit():
row[key] = int(value)
elif value.replace('.', '', 1).isdigit():
row[key] = float(value)
data.append(row)

with open(json_file, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2, ensure_ascii=False)

print(f'{csv_file} โ†’ {json_file} ๋ณ€ํ™˜ ์™„๋ฃŒ')
return data

# ์‚ฌ์šฉ Beispiel
# data = csv_to_json('users.csv', 'users.json')

Beispiel 4: JSON์—์„œ CSV๋กœ ๋ณ€ํ™˜โ€‹

import json
import csv

def json_to_csv(json_file, csv_file):
"""JSON ํŒŒ์ผ์„ CSV๋กœ ๋ณ€ํ™˜"""
with open(json_file, 'r', encoding='utf-8') as f:
data = json.load(f)

if not data:
print('๋ฐ์ดํ„ฐ๊ฐ€ ๋น„์–ด์žˆ์Šต๋‹ˆ๋‹ค')
return

# ์ฒซ ๋ฒˆ์งธ ํ•ญ๋ชฉ์—์„œ ํ‚ค ์ถ”์ถœ
if isinstance(data, list):
fieldnames = list(data[0].keys())
else:
# ๋‹จ์ผ ๊ฐ์ฒด์ธ ๊ฒฝ์šฐ
data = [data]
fieldnames = list(data[0].keys())

with open(csv_file, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(data)

print(f'{json_file} โ†’ {csv_file} ๋ณ€ํ™˜ ์™„๋ฃŒ')

# ์‚ฌ์šฉ Beispiel
# json_to_csv('users.json', 'users_output.csv')

Beispiel 5: ์—‘์…€ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ (CSV ์‚ฌ์šฉ)โ€‹

import csv
from collections import defaultdict

class SalesAnalyzer:
"""ํŒ๋งค ๋ฐ์ดํ„ฐ ๋ถ„์„๊ธฐ"""

def __init__(self, csv_file):
self.data = self.load_data(csv_file)

def load_data(self, csv_file):
"""CSV ๋ฐ์ดํ„ฐ ๋กœ๋“œ"""
data = []
with open(csv_file, 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
for row in reader:
# ์ˆซ์ž ๋ณ€ํ™˜
row['amount'] = float(row['amount'])
row['quantity'] = int(row['quantity'])
data.append(row)
return data

def total_sales(self):
"""์ด ๋งค์ถœ"""
return sum(item['amount'] for item in self.data)

def sales_by_product(self):
"""์ œํ’ˆ๋ณ„ ๋งค์ถœ"""
sales = defaultdict(float)
for item in self.data:
sales[item['product']] += item['amount']
return dict(sales)

def top_products(self, n=5):
"""์ƒ์œ„ N๊ฐœ ์ œํ’ˆ"""
sales = self.sales_by_product()
sorted_products = sorted(sales.items(), key=lambda x: x[1], reverse=True)
return sorted_products[:n]

def generate_report(self, output_file):
"""๋ฆฌํฌํŠธ ์ƒ์„ฑ"""
report = {
'total_sales': self.total_sales(),
'product_count': len(self.sales_by_product()),
'top_products': [
{'product': name, 'sales': amount}
for name, amount in self.top_products()
]
}

with open(output_file, 'w', encoding='utf-8') as f:
json.dump(report, f, indent=2, ensure_ascii=False)

return report

# ์‚ฌ์šฉ Beispiel (CSV ํŒŒ์ผ ์ƒ์„ฑ)
sample_data = [
['product', 'quantity', 'amount'],
['๋…ธํŠธ๋ถ', 5, 5000000],
['๋งˆ์šฐ์Šค', 20, 500000],
['ํ‚ค๋ณด๋“œ', 15, 1500000],
['๋…ธํŠธ๋ถ', 3, 3000000],
]

with open('sales.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerows(sample_data)

# analyzer = SalesAnalyzer('sales.csv')
# print(f'์ด ๋งค์ถœ: {analyzer.total_sales():,}์›')
# print(f'์ œํ’ˆ๋ณ„ ๋งค์ถœ: {analyzer.sales_by_product()}')
# analyzer.generate_report('sales_report.json')

Beispiel 6: ์ค‘์ฒฉ๋œ JSON ์ฒ˜๋ฆฌโ€‹

import json

def flatten_json(nested_json, parent_key='', separator='_'):
"""์ค‘์ฒฉ๋œ JSON์„ ํ‰ํƒ„ํ™”"""
items = []

for key, value in nested_json.items():
new_key = f'{parent_key}{separator}{key}' if parent_key else key

if isinstance(value, dict):
items.extend(flatten_json(value, new_key, separator).items())
elif isinstance(value, list):
for i, item in enumerate(value):
if isinstance(item, dict):
items.extend(flatten_json(item, f'{new_key}_{i}', separator).items())
else:
items.append((f'{new_key}_{i}', item))
else:
items.append((new_key, value))

return dict(items)

# ์‚ฌ์šฉ Beispiel
nested_data = {
'name': 'ํ™๊ธธ๋™',
'address': {
'city': '์„œ์šธ',
'street': '๊ฐ•๋‚จ๋Œ€๋กœ',
'zipcode': '12345'
},
'phones': [
{'type': 'home', 'number': '02-1234-5678'},
{'type': 'mobile', 'number': '010-1234-5678'}
]
}

flat = flatten_json(nested_data)
print(json.dumps(flat, indent=2, ensure_ascii=False))

Beispiel 7: CSV ๋ฐ์ดํ„ฐ ํ•„ํ„ฐ๋ง ๋ฐ ์ •๋ ฌโ€‹

import csv

class CSVProcessor:
"""CSV ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๊ธฐ"""

def __init__(self, filename):
self.filename = filename
self.data = self.load()

def load(self):
"""CSV ๋กœ๋“œ"""
with open(self.filename, 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
return list(reader)

def filter(self, condition):
"""์กฐ๊ฑด์— ๋งž๋Š” ํ–‰๋งŒ ํ•„ํ„ฐ๋ง"""
return [row for row in self.data if condition(row)]

def sort(self, key, reverse=False):
"""์ •๋ ฌ"""
return sorted(self.data, key=lambda x: x[key], reverse=reverse)

def save(self, data, output_file):
"""๊ฒฐ๊ณผ ์ €์žฅ"""
if not data:
print('์ €์žฅํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค')
return

fieldnames = list(data[0].keys())

with open(output_file, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(data)

print(f'{output_file} ์ €์žฅ ์™„๋ฃŒ')

# ์‚ฌ์šฉ Beispiel (์ƒ˜ํ”Œ CSV ์ƒ์„ฑ)
sample_data = [
['name', 'age', 'city', 'salary'],
['ํ™๊ธธ๋™', '30', '์„œ์šธ', '50000'],
['๊น€์ฒ ์ˆ˜', '25', '๋ถ€์‚ฐ', '40000'],
['์ด์˜ํฌ', '35', '์„œ์šธ', '60000'],
['๋ฐ•๋ฏผ์ˆ˜', '28', '๋Œ€๊ตฌ', '45000'],
]

with open('employees.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerows(sample_data)

# processor = CSVProcessor('employees.csv')

# ์„œ์šธ์— ์‚ฌ๋Š” ์ง์›๋งŒ ํ•„ํ„ฐ๋ง
# seoul_employees = processor.filter(lambda row: row['city'] == '์„œ์šธ')
# processor.save(seoul_employees, 'seoul_employees.csv')

# ๋‚˜์ด๋กœ ์ •๋ ฌ
# sorted_by_age = processor.sort('age', reverse=True)
# processor.save(sorted_by_age, 'employees_sorted.csv')

Beispiel 8: JSON ์Šคํ‚ค๋งˆ ๊ฒ€์ฆโ€‹

import json

class JSONValidator:
"""๊ฐ„๋‹จํ•œ JSON ์Šคํ‚ค๋งˆ ๊ฒ€์ฆ๊ธฐ"""

@staticmethod
def validate_user(data):
"""์‚ฌ์šฉ์ž ๋ฐ์ดํ„ฐ ๊ฒ€์ฆ"""
required_fields = ['name', 'email', 'age']
errors = []

# ํ•„์ˆ˜ ํ•„๋“œ ํ™•์ธ
for field in required_fields:
if field not in data:
errors.append(f'ํ•„์ˆ˜ ํ•„๋“œ ๋ˆ„๋ฝ: {field}')

# ํƒ€์ž… ํ™•์ธ
if 'name' in data and not isinstance(data['name'], str):
errors.append('name์€ ๋ฌธ์ž์—ด์ด์–ด์•ผ ')

if 'age' in data:
try:
age = int(data['age'])
if age < 0 or age > 150:
errors.append('age๋Š” 0-150 ์‚ฌ์ด์—ฌ์•ผ ')
except ValueError:
errors.append('age๋Š” ์ˆซ์ž์—ฌ์•ผ ')

if 'email' in data and '@' not in data['email']:
errors.append('์œ ํšจํ•˜์ง€ ์•Š์€ ์ด๋ฉ”์ผ')

return len(errors) == 0, errors

@staticmethod
def validate_batch(data_list):
"""์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ๊ฒ€์ฆ"""
results = []

for i, data in enumerate(data_list):
is_valid, errors = JSONValidator.validate_user(data)
results.append({
'index': i,
'valid': is_valid,
'errors': errors,
'data': data
})

return results

# ์‚ฌ์šฉ Beispiel
users = [
{'name': 'ํ™๊ธธ๋™', 'email': 'hong@example.com', 'age': 30},
{'name': '๊น€์ฒ ์ˆ˜', 'email': 'invalid-email', 'age': 25},
{'email': 'lee@example.com', 'age': 28}, # name ๋ˆ„๋ฝ
{'name': '๋ฐ•๋ฏผ์ˆ˜', 'email': 'park@example.com', 'age': 200}, # ์ž˜๋ชป๋œ age
]

results = JSONValidator.validate_batch(users)

for result in results:
print(f"\nํ•ญ๋ชฉ {result['index']}:")
if result['valid']:
print(' โœ“ ์œ ํšจ')
else:
print(' โœ— ์œ ํšจํ•˜์ง€ ์•Š์Œ')
for error in result['errors']:
print(f' - {error}')

Beispiel 9: ๋Œ€์šฉ๋Ÿ‰ JSON ์ŠคํŠธ๋ฆฌ๋ฐโ€‹

import json

def process_large_json_file(filename):
"""๋Œ€์šฉ๋Ÿ‰ JSON ํŒŒ์ผ์„ ์ฒญํฌ ๋‹จ์œ„๋กœ ์ฒ˜๋ฆฌ"""
with open(filename, 'r', encoding='utf-8') as f:
# JSON ๋ฐฐ์—ด์„ ์ŠคํŠธ๋ฆฌ๋ฐ์œผ๋กœ ์ฒ˜๋ฆฌ
decoder = json.JSONDecoder()
buffer = ''

for line in f:
buffer += line
try:
# JSON ๊ฐ์ฒด ํŒŒ์‹ฑ ์‹œ๋„
obj, index = decoder.raw_decode(buffer)
yield obj

# ํŒŒ์‹ฑ๋œ ๋ถ€๋ถ„ ์ œ๊ฑฐ
buffer = buffer[index:].lstrip()
except json.JSONDecodeError:
# ์•„์ง ์™„์ „ํ•œ ๊ฐ์ฒด๊ฐ€ ์•„๋‹˜, ๊ณ„์† ์ฝ๊ธฐ
continue

# ์‚ฌ์šฉ Beispiel
# for item in process_large_json_file('large_data.json'):
# process_item(item)

Beispiel 10: CSV์™€ JSON ํ†ตํ•ฉ ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌโ€‹

import csv
import json
from pathlib import Path

class DataManager:
"""CSV์™€ JSON์„ ํ†ตํ•ฉ ๊ด€๋ฆฌํ•˜๋Š” Klasse"""

def __init__(self, data_dir='data'):
self.data_dir = Path(data_dir)
self.data_dir.mkdir(exist_ok=True)

def save(self, data, filename, format='json'):
"""๋ฐ์ดํ„ฐ ์ €์žฅ (JSON ๋˜๋Š” CSV)"""
filepath = self.data_dir / filename

if format == 'json':
with open(filepath, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2, ensure_ascii=False)
elif format == 'csv':
if not data:
return

with open(filepath, 'w', newline='', encoding='utf-8') as f:
if isinstance(data[0], dict):
writer = csv.DictWriter(f, fieldnames=data[0].keys())
writer.writeheader()
writer.writerows(data)
else:
writer = csv.writer(f)
writer.writerows(data)

print(f'{filepath} ์ €์žฅ ์™„๋ฃŒ')

def load(self, filename):
"""๋ฐ์ดํ„ฐ ๋กœ๋“œ (์ž๋™ ํ˜•์‹ ๊ฐ์ง€)"""
filepath = self.data_dir / filename

if not filepath.exists():
print(f'ํŒŒ์ผ์ด ์—†์Šต๋‹ˆ๋‹ค: {filepath}')
return None

# ํ™•์žฅ์ž๋กœ ํ˜•์‹ ํŒ๋‹จ
if filepath.suffix == '.json':
with open(filepath, 'r', encoding='utf-8') as f:
return json.load(f)
elif filepath.suffix == '.csv':
with open(filepath, 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
return list(reader)

return None

def convert(self, input_file, output_file):
"""ํŒŒ์ผ ํ˜•์‹ ๋ณ€ํ™˜"""
data = self.load(input_file)
if data is None:
return

output_format = Path(output_file).suffix[1:] # .json โ†’ json
self.save(data, output_file, format=output_format)
print(f'{input_file} โ†’ {output_file} ๋ณ€ํ™˜ ์™„๋ฃŒ')

# ์‚ฌ์šฉ Beispiel
manager = DataManager()

# JSON ์ €์žฅ
users = [
{'id': 1, 'name': 'ํ™๊ธธ๋™', 'age': 30},
{'id': 2, 'name': '๊น€์ฒ ์ˆ˜', 'age': 25},
]
manager.save(users, 'users.json', format='json')

# CSV ์ €์žฅ
manager.save(users, 'users.csv', format='csv')

# ๋กœ๋“œ
loaded_users = manager.load('users.json')
print(loaded_users)

# ๋ณ€ํ™˜
# manager.convert('users.json', 'users_converted.csv')

Hรคufig gestellte Fragen โ“โ€‹

Q1: JSON์—์„œ ํ•œ๊ธ€์ด \uXXXX๋กœ ํ‘œ์‹œ๋˜๋Š” ๋ฌธ์ œ๋Š”?โ€‹

import json

data = {'name': 'ํ™๊ธธ๋™'}

# ๋‚˜์จ: \uXXXX๋กœ ์ธ์ฝ”๋”ฉ
json_str = json.dumps(data)
print(json_str) # {"name": "\ud64d\uae38\ub3d9"}

# ์ข‹์Œ: ํ•œ๊ธ€ ๊ทธ๋Œ€๋กœ ์œ ์ง€
json_str = json.dumps(data, ensure_ascii=False)
print(json_str) # {"name": "ํ™๊ธธ๋™"}

Q2: CSV์—์„œ ์ค„๋ฐ”๊ฟˆ์ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” Methode์€?โ€‹

import csv

# ์ค„๋ฐ”๊ฟˆ์ด ํฌํ•จ๋œ ๋ฐ์ดํ„ฐ
data = [
['name', 'description'],
['Product A', 'This is\na multi-line\ndescription'],
]

# CSV๋กœ ์ €์žฅ (์ž๋™์œผ๋กœ ์ธ์šฉ ์ฒ˜๋ฆฌ)
with open('products.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerows(data)

# ์ฝ์„ ๋•Œ๋„ ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌ
with open('products.csv', 'r', encoding='utf-8') as f:
reader = csv.reader(f)
for row in reader:
print(row)

Q3: JSON์— ์ฃผ์„์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‚˜์š”?โ€‹

# JSON ํ‘œ์ค€์€ ์ฃผ์„์„ ์ง€์›ํ•˜์ง€ ์•Š์ง€๋งŒ,
# ๋ฐ์ดํ„ฐ์— _comment ํ•„๋“œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

config = {
'_comment': '์ด๊ฒƒ์€ ์„ค์ • ํŒŒ์ผ',
'debug': True,
'max_connections': 100,
'database': {
'_comment': '๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค์ •',
'host': 'localhost',
'port': 5432
}
}

# ๋˜๋Š” JSONC, JSON5 ๊ฐ™์€ ํ™•์žฅ ํ˜•์‹ ์‚ฌ์šฉ

Q4: CSV ์ฒซ ์ค„์ด ํ—ค๋”๊ฐ€ ์•„๋‹ ๋•Œ๋Š”?โ€‹

import csv

# ํ—ค๋” ์ง์ ‘ ์ง€์ •
with open('no_header.csv', 'r', encoding='utf-8') as f:
reader = csv.DictReader(f, fieldnames=['name', 'age', 'city'])
for row in reader:
print(row)

Q5: JSON ํŒŒ์ผ์ด ๊นจ์กŒ๋Š”์ง€ ํ™•์ธํ•˜๋Š” Methode์€?โ€‹

import json

def is_valid_json(filename):
"""JSON ํŒŒ์ผ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ"""
try:
with open(filename, 'r', encoding='utf-8') as f:
json.load(f)
return True
except json.JSONDecodeError as e:
print(f'JSON ์—๋Ÿฌ: {e}')
return False
except FileNotFoundError:
print(f'ํŒŒ์ผ์ด ์—†์Šต๋‹ˆ๋‹ค: {filename}')
return False

# ์‚ฌ์šฉ
if is_valid_json('data.json'):
print('์œ ํšจํ•œ JSON ํŒŒ์ผ')

Performance-Tipps ๐Ÿš€โ€‹

JSON ์„ฑ๋Šฅโ€‹

import json

# ์ข‹์Œ: ํŒŒ์ผ ์ง์ ‘ ์‚ฌ์šฉ
with open('data.json', 'r') as f:
data = json.load(f) # ํŒŒ์ผ์—์„œ ์ง์ ‘ ๋กœ๋“œ

# ๋‚˜์จ: ๋ถˆํ•„์š”ํ•œ ์ค‘๊ฐ„ ๋‹จ๊ณ„
with open('data.json', 'r') as f:
content = f.read() # ์ „์ฒด๋ฅผ ๋ฌธ์ž์—ด๋กœ ์ฝ๊ณ 
data = json.loads(content) # ๋‹ค์‹œ ํŒŒ์‹ฑ

CSV ์„ฑ๋Šฅโ€‹

import csv

# ๋Œ€์šฉ๋Ÿ‰ CSV๋Š” DictReader๋ณด๋‹ค reader๊ฐ€ ๋น ๋ฆ„
# ํ•˜์ง€๋งŒ DictReader๊ฐ€ ๋” ํŽธ๋ฆฌํ•จ

# ๋น ๋ฆ„ (speichereffizient)
with open('large.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
process(row)

# ๋А๋ฆผ (๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์—)
with open('large.csv', 'r') as f:
reader = csv.reader(f)
all_rows = list(reader) # ๋ฉ”๋ชจ๋ฆฌ์— ์ „์ฒด ๋กœ๋“œ

Nรคchste Schritteโ€‹

  • ์ •๊ทœํ‘œํ˜„์‹: JSON/CSV ๋ฐ์ดํ„ฐ ๊ฒ€์ฆ๊ณผ ํŒŒ์‹ฑ
  • ํŒŒ์ผ ์ž…์ถœ๋ ฅ: ํŒŒ์ผ ์‹œ์Šคํ…œ ๊ณ ๊ธ‰ ์ž‘์—…
  • pandas: ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ ๋ถ„์„
  • requests: API์—์„œ JSON ๋ฐ์ดํ„ฐ ๋ฐ›๊ธฐ