json형태의 데이터를 스트링을 변환하기 위해 사용하는 함수인데, 한글이 들어가면 이상하게 변환되는 것 같다. 간단한 예시로 아래와 같이 보일 수 있다.
import json
dict_data = {
'str_data': '데이터',
}
text = json.dumps(dict_data)
print(text)
# 결과값 {"str_data": "\ub370\uc774\ud130"}
해결 방법으로는 ensure_ascii=False를 사용해준다.
text = json.dumps(dict_data, ensure_ascii=False)
왜 이런일이 발생할까?
유니코드의 형태로 제공해주는 파이썬의 배려이며, 그것을 ensure_ascii라는 파라미터로 on/off 할 수 있는 것이다.
실제로 파이썬 콘솔창에 '\ub370' 을 입력해보면 '데' 라는 글자가 출력되는 것을 확인할 수 있다.
'trouble-shooting' 카테고리의 다른 글
TypeError: can't subtract offset-naive and offset-aware datetimes (0) | 2022.11.22 |
---|---|
유니코드 정규화 문제 (NFC, NFD) (0) | 2022.11.21 |
[python] generator는 일회용? (map, filter와 같은 함수를 쓸 때 생기는 문제) (0) | 2022.11.11 |
TypeError: Object of type datetime is not JSON serializable (0) | 2022.11.11 |
IndexError: list index out of range (0) | 2022.11.08 |