Skip to content

Commit f56dffb

Browse files
committed
init
0 parents  commit f56dffb

File tree

7 files changed

+1041
-0
lines changed

7 files changed

+1041
-0
lines changed

.gitignore

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# ==========================================
2+
# Python
3+
# ==========================================
4+
# Byte-compiled / optimized / DLL files
5+
__pycache__/
6+
*.py[cod]
7+
*$py.class
8+
9+
# C extensions
10+
*.so
11+
12+
# Distribution / packaging
13+
.Python
14+
build/
15+
develop-eggs/
16+
dist/
17+
downloads/
18+
eggs/
19+
.eggs/
20+
lib/
21+
lib64/
22+
parts/
23+
sdist/
24+
var/
25+
wheels/
26+
share/python-wheels/
27+
*.egg-info/
28+
.installed.cfg
29+
*.egg
30+
MANIFEST
31+
32+
# PyInstaller
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.nox/
44+
.coverage
45+
.coverage.*
46+
.cache
47+
nosetests.xml
48+
coverage.xml
49+
*.cover
50+
*.py,cover
51+
.hypothesis/
52+
.pytest_cache/
53+
cover/
54+
55+
# Translations
56+
*.mo
57+
*.pot
58+
59+
# Environments
60+
.env
61+
.venv
62+
env/
63+
venv/
64+
ENV/
65+
env.bak/
66+
venv.bak/
67+
68+
# mypy
69+
.mypy_cache/
70+
.dmypy.json
71+
dmypy.json
72+
73+
# Pyre type checker
74+
.pyre/
75+
76+
# pytype static type analyzer
77+
.pytype/
78+
79+
# Jupyter Notebook
80+
.ipynb_checkpoints
81+
82+
# IPython
83+
profile_default/
84+
ipython_config.py
85+
86+
# ==========================================
87+
# Operating Systems
88+
# ==========================================
89+
# macOS
90+
.DS_Store
91+
.AppleDouble
92+
.LSOverride
93+
Icon
94+
._*
95+
.DocumentRevisions-V100
96+
.fseventsd
97+
.Spotlight-V100
98+
.TemporaryItems
99+
.Trashes
100+
.VolumeIcon.icns
101+
.com.apple.timemachine.donotpresent
102+
.AppleDB
103+
.AppleDesktop
104+
Network Trash Folder
105+
Temporary Items
106+
.apdisk
107+
108+
# Windows
109+
Thumbs.db
110+
Thumbs.db:encryptable
111+
ehthumbs.db
112+
ehthumbs_vista.db
113+
*.stackdump
114+
[Dd]esktop.ini
115+
$RECYCLE.BIN/
116+
*.cab
117+
*.msi
118+
*.msix
119+
*.msm
120+
*.msp
121+
*.lnk
122+
123+
# Linux
124+
*~
125+
.fuse_hidden*
126+
.directory
127+
.Trash-*
128+
.nfs*
129+
130+
# ==========================================
131+
# IDE and Editors
132+
# ==========================================
133+
# VS Code
134+
.vscode/
135+
!.vscode/settings.json
136+
!.vscode/tasks.json
137+
!.vscode/launch.json
138+
!.vscode/extensions.json
139+
*.code-workspace
140+
141+
# PyCharm / IntelliJ
142+
.idea/
143+
*.iml
144+
*.iws
145+
146+
# Vim
147+
*.swp
148+
*.swo
149+
150+
# Sublime Text
151+
*.sublime-*
152+
153+
# ==========================================
154+
# Project Specific
155+
# ==========================================
156+
# Generated CSV files (ignore all CSV outputs)
157+
*.csv
158+
159+
# Keep sample data files
160+
!sample_data.json
161+
162+
# Temporary files
163+
temp_*
164+
tmp_*
165+
166+
# Backup files
167+
*.bak
168+
*.backup
169+
170+
# Log files
171+
*.log

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.12

README.md

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
# JSON to CSV 변환기
2+
3+
JSON 배열을 CSV 파일로 변환하는 Python 유틸리티입니다.
4+
5+
## 기능
6+
7+
- JSON 배열의 각 객체를 CSV 행으로 변환
8+
- 모든 객체의 키를 자동으로 감지하여 CSV 헤더 생성
9+
- 첫 번째 객체의 필드 순서를 유지하여 CSV 헤더 정렬
10+
- 3가지 변환 모드: 기본, 고급 평면화, 전치 변환
11+
- 특수 구조 자동 감지 (계층적 헤더)
12+
- UTF-8 인코딩 지원 (한글 데이터 처리 가능)
13+
- Python 3.12 최적화
14+
15+
## 파일 구조
16+
17+
```
18+
json-to-csv/
19+
├── main.py # Python 변환기 (메인)
20+
├── sample_data.json # 샘플 데이터
21+
├── requirements.txt # Python 의존성
22+
├── .python-version # pyenv Python 버전 설정
23+
├── .gitignore # Git 무시 파일
24+
└── README.md # 이 파일
25+
```
26+
27+
## 사용법
28+
29+
### 기본 명령어
30+
31+
```bash
32+
# 기본 변환 (자동 감지)
33+
python main.py sample_data.json
34+
35+
# 출력 파일명 지정
36+
python main.py sample_data.json -o result.csv
37+
38+
# 도움말 보기
39+
python main.py --help
40+
```
41+
42+
### 고급 변환 모드
43+
44+
```bash
45+
# 고급 평면화 모드: 배열의 각 요소를 개별 필드로 분리
46+
python main.py sample_data.json --flatten
47+
48+
# 전치 변환 모드: 특수 구조를 계층적 헤더로 변환
49+
python main.py sample_data.json --transpose
50+
51+
# 자동 감지: 구조에 맞는 최적 모드 자동 선택 (권장)
52+
python main.py sample_data.json
53+
```
54+
55+
## 변환 모드
56+
57+
### 1. 기본 모드
58+
일반적인 JSON 배열을 CSV로 변환합니다.
59+
60+
**입력:**
61+
```json
62+
[
63+
{"id": 1, "name": "김철수", "age": 25},
64+
{"id": 2, "name": "이영희", "age": 30}
65+
]
66+
```
67+
68+
**출력:**
69+
```csv
70+
id,name,age
71+
1,김철수,25
72+
2,이영희,30
73+
```
74+
75+
### 2. 고급 평면화 모드 (`--flatten`)
76+
중첩된 객체와 배열을 개별 필드로 분리합니다.
77+
78+
**입력:**
79+
```json
80+
[
81+
{
82+
"id": 1,
83+
"skills": ["Python", "JavaScript"],
84+
"address": {"city": "서울", "district": "강남구"}
85+
}
86+
]
87+
```
88+
89+
**출력:**
90+
```csv
91+
address.city,address.district,id,skills[0],skills[1]
92+
서울,강남구,1,Python,JavaScript
93+
```
94+
95+
### 3. 전치 변환 모드 (`--transpose`)
96+
특수 구조의 배열들을 각 인덱스별로 행으로 변환합니다.
97+
98+
**입력:**
99+
```json
100+
[
101+
{
102+
"keyword": "수치분석",
103+
"data": {
104+
"months": ["2024-01", "2024-02", "2024-03"],
105+
"values": [100, 200, 150]
106+
}
107+
}
108+
]
109+
```
110+
111+
**출력:**
112+
```csv
113+
keyword,data,data
114+
,months,values
115+
수치분석,2024-01,100
116+
,2024-02,200
117+
,2024-03,150
118+
```
119+
120+
## 특징
121+
122+
1. **자동 감지**: 특수 구조를 자동으로 인식하여 최적 변환 모드 선택
123+
2. **필드 순서 유지**: 첫 번째 객체의 필드 순서대로 CSV 헤더 정렬
124+
3. **3가지 변환 모드**: 기본, 고급 평면화, 전치 변환
125+
4. **계층적 헤더**: 전치 모드에서 중첩 구조를 계층적으로 표현
126+
5. **유연한 스키마**: 각 객체가 다른 필드를 가져도 자동 처리
127+
6. **한글 완벽 지원**: UTF-8 인코딩으로 한글 데이터 처리
128+
7. **상세한 오류 처리**: 명확한 오류 메시지와 예외 처리
129+
130+
## 테스트
131+
132+
포함된 샘플 데이터로 테스트해보세요:
133+
134+
```bash
135+
# 기본 변환 테스트
136+
python main.py sample_data.json
137+
138+
# 제품 데이터 테스트
139+
python main.py sample_data.json
140+
141+
# 고급 평면화 테스트
142+
python main.py sample_data.json --flatten
143+
144+
# 특수 구조 변환 테스트 (자동 감지)
145+
python main.py sample_data.json
146+
147+
# 전치 변환 테스트 (수동 지정)
148+
python main.py sample_data.json --transpose
149+
150+
# 대용량 데이터 성능 테스트
151+
python main.py sample_data.json
152+
```
153+
154+
## 설치 및 요구사항
155+
156+
### 필수 요구사항
157+
- Python 3.11 이상 (Python 3.12 권장)
158+
- 추가 의존성 없음 (표준 라이브러리만 사용)
159+
160+
### 설치
161+
162+
#### 1. Python 버전 관리 (pyenv 사용)
163+
이 프로젝트는 Python 3.12에 최적화되어 있습니다. pyenv를 사용하면 자동으로 올바른 Python 버전이 설정됩니다.
164+
165+
```bash
166+
# pyenv 설치 (macOS)
167+
brew install pyenv
168+
169+
# Python 3.12 설치
170+
pyenv install 3.12
171+
172+
# 프로젝트 클론
173+
git clone <repository-url>
174+
cd json-to-csv
175+
176+
# pyenv가 자동으로 Python 3.12를 활성화합니다 (.python-version 파일 참조)
177+
python --version # Python 3.12 출력 확인
178+
```
179+
180+
#### 2. 기본 설치
181+
```bash
182+
# Git 클론
183+
git clone <repository-url>
184+
cd json-to-csv
185+
186+
# 실행 권한 부여 (선택사항)
187+
chmod +x main.py
188+
189+
# 직접 실행
190+
./main.py sample_data.json
191+
```
192+
193+
### pyenv 설정 정보
194+
- `.python-version` 파일이 포함되어 있어 프로젝트 디렉토리에서 자동으로 Python 3.12가 활성화됩니다
195+
- pyenv가 설치되어 있다면 별도 설정 없이 최적의 Python 버전이 사용됩니다
196+
- 수동으로 Python 버전을 변경하려면: `pyenv local 3.12`
197+
198+
## 사용 예시
199+
200+
### 단순 데이터 변환
201+
```bash
202+
python main.py sample_data.json -o data.csv
203+
```
204+
205+
### 복잡한 중첩 데이터
206+
```bash
207+
python main.py sample_data.json --flatten -o flattened.csv
208+
```
209+
210+
### 특수 구조(계층적 헤더) 데이터
211+
```bash
212+
python main.py sample_data.json -o transpose.csv
213+
```
214+
215+
이 도구는 다양한 JSON 구조를 지능적으로 분석하여 가장 적절한 CSV 형태로 변환합니다.

0 commit comments

Comments
 (0)