UnicodeDecodeError, invalid continuation byte

前端 未结 10 1961
忘掉有多难
忘掉有多难 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:57

    utf-8 code error usually comes when the range of numeric values exceeding 0 to 127.

    the reason to raise this exception is:

    1)If the code point is < 128, each byte is the same as the value of the code point. 2)If the code point is 128 or greater, the Unicode string can’t be represented in this encoding. (Python raises a UnicodeEncodeError exception in this case.)

    In order to to overcome this we have a set of encodings, the most widely used is "Latin-1, also known as ISO-8859-1"

    So ISO-8859-1 Unicode points 0–255 are identical to the Latin-1 values, so converting to this encoding simply requires converting code points to byte values; if a code point larger than 255 is encountered, the string can’t be encoded into Latin-1

    when this exception occurs when you are trying to load a data set ,try using this format

    df=pd.read_csv("top50.csv",encoding='ISO-8859-1')
    

    Add encoding technique at the end of the syntax which then accepts to load the data set.

提交回复
热议问题