[๊ฟํ] Python์ผ๋ก ๋ค์ํ ๋ฐ์ดํฐ ๋ค๋ฃจ๊ธฐ
์๋ณธ ๊ฒ์๊ธ: https://velog.io/@euisuk-chung/๊ฟํ-Python์ผ๋ก-๋ค์ํ-๋ฐ์ดํฐ-๋ค๋ฃจ๊ธฐ
์๋ ํ์ธ์๐ค ์ค๋์ Python์ผ๋ก ๋ค์ํ ํ์ผ ํ์์ ๋ถ๋ฌ์ค๊ณ ์ ์ฅํ๋ ๋ฐฉ๋ฒ์ ์๊ฐํ๋ ๊ธ์ ์์ฑํด๋ณด๋ ค๊ณ ํฉ๋๋ค!
ํ์ด์ฌ์ผ๋ก ๋ฐ์ดํฐ ๋ถ์์ด๋ ๋จธ์ ๋ฌ๋ ์์ ์ ํ๋ค ๋ณด๋ฉด ์ฌ๋ฌ ์ข ๋ฅ์ ํ์ผ์ ๋ง์ฃผํ๊ฒ ๋ฉ๋๋ค. ํนํ๋ LLM(Large Language Model), aka ChatGPT ์๋์ ์ ์ด๋ค๋ฉด์ ๊ธฐ์กด์ ์ฌ์ฉํ๋ CSVํ์ผ ์ด์ธ์๋ PDF, PPT ๋ฑ ๋ค์ํ ์ ํ์ ํ์ผ์ ๋ก๋ ๋ฐ ์ ์ฅํ ์ผ์ด ๋ง์์ง๊ณ ์์ต๋๋ค!
์ ๋ ์ด๋ฒ ๊ธฐํ์ ํ๋ฒ ์ ๋ฆฌํ๊ณ ๋๊ณ ๋๊ณ ์ฐธ๊ณ ํด์ ์ฐ๋ ค๊ณ ํฉ๋๋ค!!โ ๊ฐ์ด ํ๋ฒ ์ดํด ๋ณด์์ฃ ๐
-
CSV ํ์ผ(.csv) ๋ถ๋ฌ์ค๊ธฐ์ ์ ์ฅํ๊ธฐ
CSV (Comma-Separated Values) ํ์ผ์ ๋ฐ์ดํฐ๋ฅผ ์ผํ๋ก ๊ตฌ๋ถํ์ฌ ์ ์ฅํ๋ ํ์ผ ํ์์ ๋๋ค. ์ฃผ๋ก ๋ฐ์ดํฐ ๊ตํ์ด๋ ๊ฐ๋จํ ๋ฐ์ดํฐ ์ ์ฅ ์ฉ๋๋ก ์ฌ์ฉํฉ๋๋ค.
์๋ ์ฝ๋๋ก ์ฝ๊ณ ์ ์ฅํ ์ ์์ต๋๋ค:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import csv
# CSV ํ์ผ ๋ถ๋ฌ์ค๊ธฐ
with open('example.csv', mode='r') as file:
csv_reader = csv.reader(file)
for row in csv_reader:
print(row)
# CSV ํ์ผ ์ ์ฅํ๊ธฐ
with open('example.csv', mode='w', newline='') as file:
csv_writer = csv.writer(file)
csv_writer.writerow(['Name', 'Age', 'City'])
csv_writer.writerow(['Alice', '24', 'New York'])
csv_writer.writerow(['Bob', '30', 'Los Angeles'])
-
Excel ํ์ผ(.xlsx) ๋ถ๋ฌ์ค๊ธฐ์ ์ ์ฅํ๊ธฐ
Excel ํ์ผ์ Microsoft Excel์์ ์ฌ์ฉํ๋ ์คํ๋ ๋์ํธ ํ์ผ์ ๋๋ค. ์ฌ๋ฌ ์ํธ์ ๋ค์ํ ์์์ด ๊ฐ๋ฅํฉ๋๋ค.
์๋ ์ฝ๋๋ก ์ฝ๊ณ ์ ์ฅํ ์ ์์ต๋๋ค:
1
2
3
4
5
6
7
8
import pandas as pd
# Excel ํ์ผ ๋ถ๋ฌ์ค๊ธฐ
df = pd.read_excel('example.xlsx')
print(df)
# Excel ํ์ผ ์ ์ฅํ๊ธฐ
df.to_excel('example_saved.xlsx', index=False)
-
JSON ํ์ผ(.json) ๋ถ๋ฌ์ค๊ธฐ์ ์ ์ฅํ๊ธฐ
JSON (JavaScript Object Notation) ํ์ผ์ ๋ฐ์ดํฐ๋ฅผ ๊ตฌ์กฐํ๋ ํ ์คํธ ํ์์ผ๋ก ์ ์ฅํ๋ ํ์ผ์ ๋๋ค. ์ฃผ๋ก ์น ์ ํ๋ฆฌ์ผ์ด์ ์์ ๋ฐ์ดํฐ ๊ตํ์ฉ์ผ๋ก ๋ง์ด ์ฌ์ฉ๋ฉ๋๋ค.
์๋ ์ฝ๋๋ก ์ฝ๊ณ ์ ์ฅํ ์ ์์ต๋๋ค:
1
2
3
4
5
6
7
8
9
10
import json
# JSON ํ์ผ ๋ถ๋ฌ์ค๊ธฐ
with open('example.json', 'r') as file:
data = json.load(file)
print(data)
# JSON ํ์ผ ์ ์ฅํ๊ธฐ
with open('example_saved.json', 'w') as file:
json.dump(data, file, indent=4)
-
์ด๋ฏธ์ง ํ์ผ(.png) ๋ถ๋ฌ์ค๊ธฐ์ ์ ์ฅํ๊ธฐ
์ด๋ฏธ์ง ํ์ผ์ ์ฌ์ง์ด๋ ๊ทธ๋ฆผ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ํ์ผ์ ๋๋ค. ์ฌ๋ฌ ํฌ๋งท์ด ์์ง๋ง ๋ํ์ ์ผ๋ก๋ JPG์ PNG ํ์ผ์ด ์์ต๋๋ค.
์๋ ์ฝ๋๋ก ์ฝ๊ณ ์ ์ฅํ ์ ์์ต๋๋ค:
1
2
3
4
5
6
7
8
from PIL import Image
# ์ด๋ฏธ์ง ํ์ผ ๋ถ๋ฌ์ค๊ธฐ (JPG, PNG ๋ฑ)
image = Image.open('example.png')
image.show()
# ์ด๋ฏธ์ง ํ์ผ ์ ์ฅํ๊ธฐ (JPG, PNG ๋ฑ)
image.save('example_saved.png')
-
ํ ์คํธ ํ์ผ(.txt) ๋ถ๋ฌ์ค๊ธฐ์ ์ ์ฅํ๊ธฐ
ํ ์คํธ ํ์ผ์ ์์ํ ํ ์คํธ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ํ์ผ์ ๋๋ค. ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ํ์ผ ํ์ ์ค ํ๋์ ๋๋ค.
์๋ ์ฝ๋๋ก ์ฝ๊ณ ์ ์ฅํ ์ ์์ต๋๋ค:
1
2
3
4
5
6
7
8
# ํ
์คํธ ํ์ผ ๋ถ๋ฌ์ค๊ธฐ
with open('example.txt', 'r') as file:
content = file.read()
print(content)
# ํ
์คํธ ํ์ผ ์ ์ฅํ๊ธฐ
with open('example.txt', 'w') as file:
file.write("์ด๊ฒ์ ์ ์ฅ๋ ํ
์คํธ์
๋๋ค.")
-
YAML ํ์ผ(.yaml) ๋ถ๋ฌ์ค๊ธฐ์ ์ ์ฅํ๊ธฐ
YAML (YAML Ainโt Markup Language) ํ์ผ์ ์ค์ ํ์ผ์ด๋ ๋ฐ์ดํฐ ์ง๋ ฌํ์ ์์ฃผ ์ฌ์ฉ๋๋ ํ์์ ๋๋ค. ์ฌ๋์ด ์ฝ๊ณ ์ฐ๊ธฐ ์ฝ๊ฒ ์ค๊ณ๋์์ต๋๋ค.
์๋ ์ฝ๋๋ก ์ฝ๊ณ ์ ์ฅํ ์ ์์ต๋๋ค:
1
2
3
4
5
6
7
8
9
10
import yaml
# YAML ํ์ผ ๋ถ๋ฌ์ค๊ธฐ
with open('example.yaml', 'r') as file:
data = yaml.safe_load(file)
print(data)
# YAML ํ์ผ ์ ์ฅํ๊ธฐ
with open('example_saved.yaml', 'w') as file:
yaml.safe_dump(data, file)
-
ํผํด ํ์ผ(.pkl) ๋ถ๋ฌ์ค๊ธฐ์ ์ ์ฅํ๊ธฐ
ํผํด ํ์ผ์ Python ๊ฐ์ฒด๋ฅผ ์ง๋ ฌํํ์ฌ ์ ์ฅํ๋ ํ์ผ์ ๋๋ค. ๋์ค์ ๋ค์ ๋ถ๋ฌ์ฌ ์ ์๋๋ก ํฉ๋๋ค.
์๋ ์ฝ๋๋ก ์ฝ๊ณ ์ ์ฅํ ์ ์์ต๋๋ค:
1
2
3
4
5
6
7
8
9
10
import pickle
# ํผํด ํ์ผ ๋ถ๋ฌ์ค๊ธฐ
with open('example.pkl', 'rb') as file:
data = pickle.load(file)
print(data)
# ํผํด ํ์ผ ์ ์ฅํ๊ธฐ
with open('example_saved.pkl', 'wb') as file:
pickle.dump(data, file)
-
SQLite ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ์ผ(.db) ๋ถ๋ฌ์ค๊ธฐ์ ์ ์ฅํ๊ธฐ
SQLite ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ์ผ์ ๊ฒฝ๋ SQL ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ด๋ฆฌ ์์คํ ์ธ SQLite์์ ์ฌ์ฉํ๋ ํ์ผ์ ๋๋ค. ์๊ณ ๋น ๋ฅด๋ฉฐ ๋ ๋ฆฝ์ ์ธ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ์ผ๋ก ์ฌ์ฉ๋ฉ๋๋ค.
์๋ ์ฝ๋๋ก ์ฝ๊ณ ์ ์ฅํ ์ ์์ต๋๋ค:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import sqlite3
# SQLite ํ์ผ ๋ถ๋ฌ์ค๊ธฐ
conn = sqlite3.connect('example.db')
cursor = conn.cursor()
cursor.execute("SELECT * FROM example_table")
rows = cursor.fetchall()
for row in rows:
print(row)
conn.close()
# SQLite ํ์ผ ์ ์ฅํ๊ธฐ
conn = sqlite3.connect('example.db')
cursor = conn.cursor()
cursor.execute("INSERT INTO example_table (name, age) VALUES (?, ?)", ('Charlie', 35))
conn.commit()
conn.close()
-
HDF5(.h5) ํ์ผ ๋ถ๋ฌ์ค๊ธฐ์ ์ ์ฅํ๊ธฐ
HDF5 (Hierarchical Data Format version 5) ํ์ผ์ ๋์ฉ๋ ๋ฐ์ดํฐ๋ฅผ ํจ์จ์ ์ผ๋ก ์ ์ฅํ๊ณ ๊ด๋ฆฌํ๊ธฐ ์ํ ํ์ผ ํ์์ ๋๋ค. ๊ณผํ ๋ฐ์ดํฐ์ ์์ฃผ ์ฌ์ฉ๋ฉ๋๋ค.
์๋ ์ฝ๋๋ก ์ฝ๊ณ ์ ์ฅํ ์ ์์ต๋๋ค:
1
2
3
4
5
6
7
8
9
10
import h5py
# HDF5 ํ์ผ ๋ถ๋ฌ์ค๊ธฐ
with h5py.File('example.h5', 'r') as file:
data = file['dataset_name'][:]
print(data)
# HDF5 ํ์ผ ์ ์ฅํ๊ธฐ
with h5py.File('example_saved.h5', 'w') as file:
file.create_dataset('dataset_name', data=data)
-
MATLAB ํ์ผ(.mat) ๋ถ๋ฌ์ค๊ธฐ์ ์ ์ฅํ๊ธฐ
MAT ํ์ผ์ MATLAB์์ ์ฌ์ฉํ๋ ํ์ผ ํฌ๋งท์ผ๋ก, MATLAB ์์ ํ๊ฒฝ์ ๋ณ์๋ฅผ ์ ์ฅํด์.
์๋ ์ฝ๋๋ก ์ฝ๊ณ ์ ์ฅํ ์ ์์ต๋๋ค:
1
2
3
4
5
6
7
8
from scipy.io import loadmat, savemat
# MATLAB ํ์ผ ๋ถ๋ฌ์ค๊ธฐ
data = loadmat('file.mat')
print(data)
# MATLAB ํ์ผ ์ ์ฅํ๊ธฐ
savemat('file_saved.mat', {'variable_name': data})
-
XML ํ์ผ(.xml) ๋ถ๋ฌ์ค๊ธฐ์ ์ ์ฅํ๊ธฐ
XML (eXtensible Markup Language) ํ์ผ์ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ํํํ๊ธฐ ์ํ ๋งํฌ์ ์ธ์ด๋ก, ๋ค์ํ ๋ฐ์ดํฐ ๊ตํ ํฌ๋งท์ผ๋ก ์ฌ์ฉ๋ผ์.
์๋ ์ฝ๋๋ก ์ฝ๊ณ ์ ์ฅํ ์ ์์ต๋๋ค:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import xml.etree.ElementTree as ET
# XML ํ์ผ ๋ถ๋ฌ์ค๊ธฐ
tree = ET.parse('example.xml')
root = tree.getroot()
for child in root:
print(child.tag, child.attrib)
# XML ํ์ผ ์ ์ฅํ๊ธฐ
root = ET.Element("root")
child = ET.SubElement(root, "child")
child.text = "์ด๊ฒ์ ์ ์ฅ๋ ํ
์คํธ์
๋๋ค."
tree = ET.ElementTree(root)
tree.write('example_saved.xml')
-
PPTX ํ์ผ(.pptx) ๋ถ๋ฌ์ค๊ธฐ์ ์ ์ฅํ๊ธฐ
PPTX ํ์ผ์ Microsoft PowerPoint ํ๋ ์ ํ
์ด์
ํ์ผ ํ์์ด์์. python-pptx
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํด์ ๋ถ๋ฌ์ค๊ณ ์ ์ฅํ ์ ์์ด์.
์๋ ์ฝ๋๋ก ์ฝ๊ณ ์ ์ฅํ ์ ์์ต๋๋ค:
1
2
3
4
5
6
7
8
9
10
11
from pptx import Presentation
# PPTX ํ์ผ ๋ถ๋ฌ์ค๊ธฐ
presentation = Presentation('example.pptx')
for slide in presentation.slides:
for shape in slide.shapes:
if shape.has_text_frame:
print(shape.text)
# PPTX ํ์ผ ์ ์ฅํ๊ธฐ
presentation.save('example_saved.pptx')
-
PDF ํ์ผ(.pdf) ๋ถ๋ฌ์ค๊ธฐ
PDF (Portable Document Format) ํ์ผ์ Adobe Systems์์ ๊ฐ๋ฐํ ์ ์ ๋ฌธ์ ํ์์
๋๋ค. yPDF2
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํด์ ๋ถ๋ฌ์ฌ ์ ์์ต๋๋ค.
์๋ ์ฝ๋๋ก ์ฝ๊ณ ์ ์ฅํ ์ ์์ต๋๋ค:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import PyPDF2
def load_pdf(file_path):
with open(file_path, 'rb') as file:
reader = PyPDF2.PdfFileReader(file)
pages = [reader.getPage(i) for i in range(reader.numPages)]
return pages
def create_pdf(pages):
writer = PyPDF2.PdfFileWriter()
for page in pages:
writer.addPage(page)
return writer
def save_pdf(writer, output_path):
with open(output_path, 'wb') as file:
writer.write(file)
# ์ ์ฒด ์์
input_path = 'example.pdf'
output_path = 'new_example.pdf'
# PDF ๋ถ๋ฌ์ค๊ธฐ
pages = load_pdf(input_path)
# PDF ์์ฑํ๊ธฐ
pdf_writer = create_pdf(pages)
# PDF ์ ์ฅํ๊ธฐ
save_pdf(pdf_writer, output_path)
์ด๋ ๊ฒ ๋ค์ํ ํ์ผ ํ์์ Python์ผ๋ก ๋ถ๋ฌ์ค๊ณ ์ ์ฅํ๋ ๋ฐฉ๋ฒ์ ์์๋ดค์ต๋๋ค. ์ด ๋ฐฉ๋ฒ๋ค์ ํ์ฉํ๋ฉด ์ฌ๋ฌ ์ ํ์ ๋ฐ์ดํฐ๋ฅผ ์์ฝ๊ฒ ๋ค๋ฃฐ ์ ์์ต๋๋ค.
๊ถ๊ธํ ์ ์ด๋ ์ถ๊ฐ๋ก ๋ค๋ฃจ๊ณ ์ถ์ ํ์ผ ํ์์ด ์๋ค๋ฉด ๋๊ธ๋ก ๋จ๊ฒจ์ฃผ์ธ์! ๐
๊ฐ์ฌํฉ๋๋ค~~!