반응형
파이썬, 한글 인코딩 형식 검사
글. 수알치 오상문
먼저 chardet 모듈을 설치합니다.
> pip install chardet
다음은 예제 코드입니다.
# 한글 인코딩 형식 검사
# encoding_checker.py
# by 오상문
#
# pip install chardet
import chardet
text = "안녕하세요" # 파이썬3은 기본으로 utf-8 인코딩 사용
result = chardet.detect(text.encode())
encoding = result['encoding']
print(encoding) # utf-8
cp949_byte_code = text.encode('euc-kr') # 'cp949'와 같음
print(cp949_byte_code) # b'\xbe\xc8\xb3\xe7\xc7\xcf\xbc\xbc\xbf\xe4'
result = chardet.detect(cp949_byte_code)
encoding = result['encoding']
print(encoding) # EUC-KR
decoded_text = cp949_byte_code.decode('cp949') # 원래 utf-8로 변경
print(decoded_text) # 안녕하세요
[실행 결과]
utf-8
b'\xbe\xc8\xb3\xe7\xc7\xcf\xbc\xbc\xbf\xe4'
EUC-KR
안녕하세요
반응형
'Python 활용' 카테고리의 다른 글
파이썬, 내 컴퓨터 IP 확인하기 (0) | 2023.04.18 |
---|---|
한글 문자열을 검사해서 utf-8 아니면 utf-8 인코딩으로 자동 변환 (0) | 2023.04.12 |
Python에서 Java 호출하는 방법 (0) | 2023.04.07 |
파이썬, 아이디와 비밀번호('*' 출력) 입력, 암호화, 비교, 로그인 처리 (0) | 2023.04.01 |
다음(DAUM) 이메일 서버를 이용하여 이메일 보내기 (0) | 2023.03.30 |