UnicodeDecodeError: 'utf-8' codec can't decode byte

AI빅데이터|2020. 3. 15. 10:18
반응형

다음과 같이 subprocess에서 받아온 값에 한글이 포함될 경우,

Abaconda prompt에서 py를 실행할 경우, 제목과 같은 오류가 발생한다.

 

<코드>

RLTrader = subprocess.Popen([envPath, exeFilePath], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=subprocess_flags)
(stdout, stderr) = RLTrader.communicate(input=transData)
print("aftershock" + str(stdout))
RLTrader_results = stdout
RLTrader_results02 = RLTrader_results.decode()

 

<실행결과>

aftershockb'\xb0\xfc\xb8\xc1&&&0\r\n'
Traceback (most recent call last):
  File "pytrader4.py", line 517, in 
    SH.Start_of_RLTrader()
  File "pytrader4.py", line 294, in Start_of_RLTrader
    self.Requirement_of_current()
  File "pytrader4.py", line 425, in Requirement_of_current
    RLTrader_results02 = RLTrader_results.decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 0: invalid start byte

 

 

RLTrader = subprocess.Popen([envPath, exeFilePath], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=subprocess_flags)
(stdout, stderr) = RLTrader.communicate(input=transData)
print("aftershock" + str(stdout))
RLTrader_results = stdout 
RLTrader_results02 = RLTrader_results.decode(encoding='CP949')

 

이렇게 수정해 주니, 에러가 없어졌네요..

반응형

댓글()