본문 바로가기

D.S/Python

210812목 - python json 파일에 write할 때 글자 깨짐 문제

728x90

 

문제

기록된 json파일에 제대로 된 글자가 나오지 않는다. 한글이 \u20ac  같은 유니코드로 찍혀나옴.

 

 

해결

File을 open할 때도 encoding=’utf-8’처리를 해주고, json.dump 처리할 때도 ensure_ascii=False 처리해준다.

with open("example.json","w", encoding='utf-8') as jsonfile: 
json.dump(data,jsonfile,ensure_ascii=False)

 

 

 

참조

https://stackoverflow.com/questions/16291358/python-saving-json-files-as-utf-8

https://stackoverflow.com/questions/18337407/saving-utf-8-texts-with-json-dumps-as-utf8-not-as-u-escape-sequence

 

반응형