개발자 모드/파이썬(python)

파이썬 UnicodeDecodeError: 'cp949' codec can't decode byte 0xec in position 23: illegal multibyte sequence 에러 시...

인생은직구 2021. 5. 20. 16:57
728x90

test2.yaml
0.00MB
yml_sample6.pyw
0.00MB

아래와 같이 yaml파일과 python code 실행시  아래와 같은 Error 메세지를 발생하며 실행되지 않습니다.

 

Warning (from warnings module):
  File "C:\Users\byksr1107\Documents\python\yml_test\yml_sample5.pyw", line 12
    doc = yaml.load(open('test2.yaml', 'r'))
YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
Traceback (most recent call last):
  File "C:\Users\byksr1107\Documents\python\yml_test\yml_sample5.pyw", line 12, in <module>
    doc = yaml.load(open('test2.yaml', 'r'))
  File "C:\Users\byksr1107\AppData\Local\Programs\Python\Python39\lib\site-packages\yaml\__init__.py", line 112, in load
    loader = Loader(stream)
  File "C:\Users\byksr1107\AppData\Local\Programs\Python\Python39\lib\site-packages\yaml\loader.py", line 24, in __init__
    Reader.__init__(self, stream)
  File "C:\Users\byksr1107\AppData\Local\Programs\Python\Python39\lib\site-packages\yaml\reader.py", line 85, in __init__
    self.determine_encoding()
  File "C:\Users\byksr1107\AppData\Local\Programs\Python\Python39\lib\site-packages\yaml\reader.py", line 124, in determine_encoding
    self.update_raw()
  File "C:\Users\byksr1107\AppData\Local\Programs\Python\Python39\lib\site-packages\yaml\reader.py", line 178, in update_raw
    data = self.stream.read(size)
UnicodeDecodeError: 'cp949' codec can't decode byte 0xec in position 23: illegal multibyte sequence
>>> 

 

 

 

 

test2.yaml 

strs:
  - 철수
  - 영희
  - 명수

 

python code

#-*- coding: utf-8 -*-

import yaml
import wx
import os
import sys

#sys.setdefaultencoding('utf-8')


 
doc = yaml.load(open('test2.yaml', 'r'))
strs = doc['strs']
 
for s in strs:
    print(s)  # 철수, 영희, 명수

if __name__ == '__main__':
    app = wx.PySimpleApp()
    dlg = wx.SingleChoiceDialog(None,
        'What version of Python are you using?',
        'Single Choice',
        strs,
        wx.CHOICEDLG_STYLE)
        
    if dlg.ShowModal() == wx.ID_OK:
        response = dlg.GetStringSelection()
 
    print(response)
    dlg.Destroy()
    app.MainLoop()

 

 

위의 소스가 실행이 되지 않는 이유는 file open시 한글을 지원하지 않기 때문에 에러가 발생한다

그래서 파일 open 부분을 

 

doc = yaml.load(open('test2.yaml', 'r',encoding='utf-8'))

로 바꾸어주면 된다

 

#-*- coding: utf-8 -*-

import yaml
import wx
import os
import sys

#sys.setdefaultencoding('utf-8')


 
doc = yaml.load(open('test2.yaml', 'r',encoding='utf-8'))
strs = doc['strs']
 
for s in strs:
    print(s)  # 철수, 영희, 명수

if __name__ == '__main__':
    app = wx.PySimpleApp()
    dlg = wx.SingleChoiceDialog(None,
        'What version of Python are you using?',
        'Single Choice',
        strs,
        wx.CHOICEDLG_STYLE)
        
    if dlg.ShowModal() == wx.ID_OK:
        response = dlg.GetStringSelection()
 
    print(response)
    dlg.Destroy()
    app.MainLoop()


 

결과

 

 

 

728x90