Python JSON 파일(.json) 저장 및 불러오기

2022. 3. 3. 18:10·Programming/Python

 

Python의 dictionary type과 JSON 파일(.json)은 호환이 가능하다!

저장

import json

dictionary_example = {'장소명': '경복궁',
			'주소': '서울 종로구 사직로 161 경복궁',
                    '전화번호': '02-3700-3900',
                    '홈페이지': 'http://www.royalpalace.go.kr/'}

file_path = "C:/Users/user/Desktop/test/"

with open(file_path + "test.json", 'w') as f:
	json.dump(dictionary_example, f)

 

영어만을 저장하면 문제가 없지만, 한글이 포함되어있는 경우에는 아래와 같이 유니코드 16진수로 표현됨

{"\uc7a5\uc18c\uba85": "\uacbd\ubcf5\uad81",
 "\uc8fc\uc18c": "\uc11c\uc6b8 \uc885\ub85c\uad6c \uc0ac\uc9c1\ub85c 161 \uacbd\ubcf5\uad81",
 "\uc804\ud654\ubc88\ud638": "02-3700-3900",
 "\ud648\ud398\uc774\uc9c0": "http://www.royalpalace.go.kr/"}

 

데이터를 한글로 저장하려면, 다음의 옵션 추가

 - with open 행에 enconding='UTF-8'

 - json.dump 행에 ensure_ascii=False

with open(file_path + "test.json", 'w', encoding='UTF-8') as f:
	json.dump(dictionary_example, f, ensure_ascii=False)

 

이렇게 하고 JSON파일을 열어보면, 아래와 같이 한글이 제대로 표시된다!

{"장소명": "경복궁",
 "주소": "서울 종로구 사직로 161 경복궁",
 "전화번호": "02-3700-3900",
 "홈페이지": "http://www.royalpalace.go.kr/"}

 

불러오기

import json

file_path = "C:/Users/user/Desktop/test/"

with open(file_path + "test.json", 'r') as f:
	load_test = json.load(f)

 

마찬가지로 영어라면 제대로 불러와지겠지만, 한글이 포함되어 있으면 오류가 발생함

 

불러올때도 다음의 옵션 추가가 필요

 - with open 행에 enconding='UTF-8'

with open(file_path + "test.json", 'r', encoding='UTF-8') as f:
	load_test = json.load(f)

 

이렇게 하면 한글도 제대로 불러와지고, 불러온 JSON 파일을 열어보면 type이 dictionary인 것을 알 수 있음

print(load_test)
print(type(load_test))
{'장소명': '경복궁',
 '주소': '서울 종로구 사직로 161 경복궁',
 '전화번호': '02-3700-3900',
 '홈페이지': 'http://www.royalpalace.go.kr/'}
<class 'dict'>

 

저작자표시 비영리 변경금지 (새창열림)

'Programming > Python' 카테고리의 다른 글

[Python] if __name__ == '__main__':  (0) 2022.04.25
[Python] py ipynb 변환 (Jupyter Notebook 이용)  (2) 2022.04.12
Python Numpy Tutorial (with Jupyter and Colab)  (1) 2022.02.15
[Python] 객체지향 - super()  (0) 2021.11.15
[Python] 객체지향 - 클래스, 객체, 생성자  (0) 2021.11.15
'Programming/Python' 카테고리의 다른 글
  • [Python] if __name__ == '__main__':
  • [Python] py ipynb 변환 (Jupyter Notebook 이용)
  • Python Numpy Tutorial (with Jupyter and Colab)
  • [Python] 객체지향 - super()
카이로셜
카이로셜
  • 카이로셜
    카이로스의 시간
    카이로셜
  • 글쓰기 관리
  • 전체
    오늘
    어제
    • 분류 전체보기
      • Programming
        • Python
        • Linux
        • Git, Github
        • ML, Machine Learning
        • DL, Deep Learning
        • NLP
        • Computer Vision
        • Network
        • PyCharm
      • IT
        • Windows
        • Mac OS
        • Programs
        • 한글
        • Word
        • Excel
        • PowerPoint
      • Math & Statistics
      • English
      • Graduate School
      • etc.
      • Record
  • 블로그 메뉴

    • Github
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    윈도우10
    윈도우11
    아나콘다 가상환경
    hotkey
    객체
    맥북 단축키
    docker
    도커
    anaconda
    클래스
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
카이로셜
Python JSON 파일(.json) 저장 및 불러오기
상단으로

티스토리툴바