NLP/AI/Statistics

python json 파일 저장 및 불러오기 본문

Computer Setting/Python & ETC

python json 파일 저장 및 불러오기

Danbi Cho 2021. 6. 9. 15:01

dictionary를 저장하기 위해 json 파일을 생성하였다. 

 

우선, json 저장 방법은 다음과 같다. 

 

$ import json

$ with open('./file_name.json', 'w') as f:
$ 	json.dumps(dictionary, f)


# or 

$ save_file = open('./file_name.json','w')
$ json.dump(dictionary, save_file)
$ save_file.close()

 

dictionary를 'file_name'이라는 파일명으로 저장하려고 할 때 위의 코드와 두 가지 방법으로 저장할 수 있다. 

 

저장한 json 파일을 불러오기 위한 코드는 다음과 같다. 

 

$ with open('./file_name.json', 'r') as f:
$	json_data = json.load(f)
$	json_data = json.dumps(json_data)

위와 같이 불러오면 json_data라는 변수명으로 dictionary가 불러와진다. 

 

'Computer Setting > Python & ETC' 카테고리의 다른 글

python 지수 표현 변경  (0) 2021.06.11
python json 파일 한글 깨짐 현상  (0) 2021.06.09
docker 비밀번호 변경  (0) 2020.10.22
python default version 변경  (0) 2020.10.22
Virtualenv 생성  (0) 2020.10.22
Comments