How to fix: “UnicodeDecodeError: 'ascii' codec can't decode byte”

前端 未结 19 1582
谎友^
谎友^ 2020-11-22 01:21
as3:~/ngokevin-site# nano content/blog/20140114_test-chinese.mkd
as3:~/ngokevin-site# wok
Traceback (most recent call last):
File \"/usr/local/bin/wok\", line 4, in
         


        
19条回答
  •  青春惊慌失措
    2020-11-22 02:04

    This error occurs when there are some non ASCII characters in our string and we are performing any operations on that string without proper decoding. This helped me solve my problem. I am reading a CSV file with columns ID,Text and decoding characters in it as below:

    train_df = pd.read_csv("Example.csv")
    train_data = train_df.values
    for i in train_data:
        print("ID :" + i[0])
        text = i[1].decode("utf-8",errors="ignore").strip().lower()
        print("Text: " + text)
    

提交回复
热议问题