UnicodeDecodeError, invalid continuation byte

前端 未结 10 2120
忘掉有多难
忘掉有多难 2020-11-22 08:25

Why is the below item failing? Why does it succeed with "latin-1" codec?

o = "a test of \\xe9 char" #I want this to remain a string as thi         


        
10条回答
  •  轮回少年
    2020-11-22 08:54

    Because UTF-8 is multibyte and there is no char corresponding to your combination of \xe9 plus following space.

    Why should it succeed in both utf-8 and latin-1?

    Here how the same sentence should be in utf-8:

    >>> o.decode('latin-1').encode("utf-8")
    'a test of \xc3\xa9 char'
    

提交回复
热议问题