Last active
July 5, 2024 15:32
-
-
Save ohahohah/f9fb38e82e7c4a3ac7f63b2c90e84f2b to your computer and use it in GitHub Desktop.
json 파일들 불러와서 csv로 저장하기
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## json 파일들 불러와서 csv로 저장하기\n", | |
"\n", | |
"합칠 json 파일들을 준비합니다. 현재 이 파일이 있는 곳을 기준으로 파일들을 불러옵니다. 하위폴더 밑에 있는 모든 파일을 불러옵니다.(기본적으로 하위의 하위 폴더 내 파일 모두)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"pycharm": { | |
"is_executing": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"from pathlib import Path\n", | |
"import json\n", | |
"import pandas as pd\n", | |
"\n", | |
"def get_filelist(subfolder, file_extension):\n", | |
" data_path = Path.cwd()/subfolder\n", | |
" \n", | |
" return list(data_path.glob('**/*' + file_extension))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"pycharm": { | |
"is_executing": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"# 이 파일이 위치해있는 폴더의 하위폴더 'data'에 있는 확장자명이 '.json'인 모든 파일을 불러옵니다\n", | |
"files = get_filelist('data','json')\n", | |
"\n", | |
"# 저장할 데이터 항목의 이름을 입력합니다. json 파일에 적힌 항목(key)과 같아야합니다.\n", | |
"column_names = ['create_date','location_id','location_name','md101_sn','msg','send_platform']\n", | |
"result = pd.DataFrame(columns=column_names) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"pycharm": { | |
"is_executing": false | |
} | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" create_date location_id location_name md101_sn \\\n", | |
"0 2020/03/04 14:07:20 93 경상북도 의성군 28522 \n", | |
"1 2020/03/04 14:06:48 93 경상북도 의성군 28521 \n", | |
"\n", | |
" msg send_platform \n", | |
"0 [의성군청]의성관외4번환자(주민등록주소지:경산,실거주지:금성면)이동경로3/2 13:... cbs \n", | |
"1 [의성군청]의성관외4번환자(주민등록주소지:경산, 실거주지:금성면)이동경로 2/22 ... cbs \n", | |
" create_date location_id location_name md101_sn \\\n", | |
"0 03 93 경상북도 의성군 1 \n", | |
"1 03 93 경상북도 의성군 2 \n", | |
"\n", | |
" msg send_platform \n", | |
"0 [의성군청]의성관외4번환자(주민등록주소지:경산,실거주지:금성면)이동경로3/2 13:... cbs \n", | |
"1 [의성군청]의성관외4번환자(주민등록주소지:경산, 실거주지:금성면)이동경로 2/22 ... cbs \n", | |
" create_date location_id location_name md101_sn \\\n", | |
"0 02 93 경상북도 의성군 1 \n", | |
"1 02 93 경상북도 의성군 2 \n", | |
"\n", | |
" msg send_platform \n", | |
"0 [의성군청]의성관외4번환자(주민등록주소지:경산,실거주지:금성면)이동경로3/2 13:... cbs \n", | |
"1 [의성군청]의성관외4번환자(주민등록주소지:경산, 실거주지:금성면)이동경로 2/22 ... cbs \n", | |
" create_date location_id location_name md101_sn \\\n", | |
"0 04 93 경상북도 의성군 1 \n", | |
"1 04 93 경상북도 의성군 2 \n", | |
"\n", | |
" msg send_platform \n", | |
"0 [의성군청]의성관외4번환자(주민등록주소지:경산,실거주지:금성면)이동경로3/2 13:... cbs \n", | |
"1 [의성군청]의성관외4번환자(주민등록주소지:경산, 실거주지:금성면)이동경로 2/22 ... cbs \n" | |
] | |
} | |
], | |
"source": [ | |
"for json_file in files:\n", | |
" df = pd.read_json(json_file)\n", | |
" row_data = pd.json_normalize(data=df['row'])\n", | |
" print(row_data.head(2)) #데이터가 잘 불러와지는지 확인하는 출력\n", | |
" \n", | |
" result = pd.concat([result,df])\n", | |
" \n", | |
"# 현재 이 파일이 위치한 폴더의 하위 폴더 data 에 'result.csv'로 저장\n", | |
"result.to_csv(Path.cwd()/'data'/'result.csv', index=None)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.6" | |
}, | |
"pycharm": { | |
"stem_cell": { | |
"cell_type": "raw", | |
"metadata": { | |
"collapsed": false | |
}, | |
"source": [] | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment