CS
json.dump와 json.dumps 차이
zhelddustmq
2024. 9. 27. 21:17
json.dumps()
Python dict object를 JSON 문자열로 변환할 수 있습니다
링크: https://docs.python.org/3/library/json.html#json.dumps
문법:
json.dumps(dict, indent)
인자
dict – dictionary 의 이름
indent – 들여쓰기 숫자
예제
import json
dictionary = {
"id": "01",
"name": "Kim",
"department": "HR"
}
json_object = json.dumps(dictionary, indent=4)
print(json_object)
결과
{
"department": "HR",
"id": "01",
"name": "Kim"
}
json.dump()
json.dump() 메서드는 JSON 파일에 write 하는데 사용할 수 있습니다.
링크: https://docs.python.org/3/library/json.html#json.dump
문법:
json.dump(dict, file_pointer)
인자:
dict – dictionary 이름
file pointer – 저장하고자 하는 파일 object
예제
import json
dictionary = {
"id": "04",
"name": "Jun",
"department": "HR"
}
with open("test.json", "w") as outfile:
json.dump(dictionary, outfile)
결과(test.json 파일)
{"department": "HR", "id": "04", "name": "Jun"}
더 정확한 자료는?: https://wikidocs.net/126088