python/모듈

[Python] json - 문자열을 딕셔너리로 변환

wjwkddyd221001 2022. 12. 17. 17:11

개요

json 모듈을 이용하여 string을 dictionary로 변환할 수 있다.

 

예시

  • 문자열(string)을 딕셔너리(dictionary)로 변환
import json

string = "{'a':1, 'b':2, 'c':3}"
dictionary = json.loads(string.replace("'","\"")) # 작은 따옴표 > 큰 따옴표로 바꿈


print(dictionary) # {'a': 1, 'b': 2, 'c': 3}
print(type(dictionary)) # <class 'dict'>