반응형

오류 내용

아래 코드에서 오류 발생

cmd = 'ping 172.20.1.1'
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)

while True:
    line = process.stdout.readline()
    print(line.decode())
    if not line:
        break

 

'utf-8' codec can't decode byte 0xb9 in position 21: invalid start byte

 

 

오류 원인과 해결 방법

bytes를 유니코드로 출력하기 위해 decode 함수를 사용했으나 decode의 기본 인코딩 방식인 utf-8이 한글을 지원하지 않아서 발생한 문제.

decode()를 decode('euc-kr') 또는 decode('cp494')로 수정하여 해결

 

 

참고

2022.09.05 - [언어, 라이브러리/Python] - [Python] 바이트형(bytes, b'')의 문자열 변환(decode)