Python Json 여러 파일 읽기

Jyo·2022년 11월 4일

Python

목록 보기
1/5
post-thumbnail

Json 파일 쓰기

path = [path]
with open(path, "w", encoding="utf-8") as f:
	json.dump([df], f, ensure_ascii=False)

Json 파일 읽기

path = [path]
with open(path, 'r', encoding='utf-8') as f:
	df = json.load(f)

Json 데이터 여러개 불러오기

우선 import os 를 통해 json 원하는 폴더내에 json 폴더들을 가져옵니다.

import os

path = './[path]' 
#[path] 에 데이터가 있는 디렉터리

file_list = os.listdir(path)
# 경로 내에 파일을 모두 불러옴

json_file_list = [file for file in file_list if file.endswith('.json')
#json 확장자를 가진 파일로 새로운 리스트 생성

그러면 json_file_list 에는 json 파일들만 남아있게 됩니다.

이제 json 라이브러리를 사용하여 데이터를 불러와주면 됩니다.

with open([file], 'r') as f:  #[file]에 파일 제목 입력
	file = json.load(f)

이제 두 가지를 같이 사용합니다.
json_file_list 를 통해 json 파일들을 불러옵니다.

DataFrame = []
for json in json_file_list:
	DataFrame.append(json.load(f))

이제 DataFrame 에서 Index 를 접근하여 필요한 데이터를 사용하면 됩니다.

profile
Jyo

0개의 댓글