HappyWeasel
파이썬 - json 본문
# json 패키지를 임포트합니다.
import json
#JSON 파일을 읽고 문자열을 딕셔너리로 변환합니다.
def create_dict(filename):
with open(filename) as file:
json_string = file.read()
return json.loads(json_string)
#JSON 파일을 읽고 딕셔너리를 JSON 형태의 문자열로 변환합니다.
def create_json(dictionary, filename):
with open(filename, 'w') as file:
# 함수를 완성하세요.
json_string = json.dumps(dictionary)
file.write(json_string)
# 아래 주석을 해제하고 결과를 확인해보세요.
src = 'netflix.json'
dst = 'new_netflix.json'
netflix_dict = create_dict(src)
print('원래 데이터: ' + str(netflix_dict))
netflix_dict['Dark Knight'] = 39217
create_json(netflix_dict, dst)
updated_dict = create_dict(dst)
print('수정된 데이터: ' + str(updated_dict))
json.loads(json) : json -> string
json.dumps(string) : string -> json
'Basic > Python' 카테고리의 다른 글
파이썬 - csv 파일 다루기 (0) | 2020.06.04 |
---|---|
파이썬 - set (0) | 2020.06.04 |
파이썬 - 그래프 다루기 (0) | 2020.06.04 |
파이썬 - 데이터 정렬하기 (0) | 2020.06.03 |
파이썬 - 코드 간결하게 (0) | 2020.06.03 |
Comments