Python出现UnicodeDecodeError: 'gbk' codec can't decode byte 0xbb in position 8: illegal multibyte sequence错误

原因是在读取文件时由于编码方式不同无法进行转换

出错代码:

 for line in open('keji.txt', 'r').readlines():
        print(line)
        corpus.append(line.strip())
    # print corpus
    time.sleep(5)

解决方法,在打开文件语句中加入编码方式,将其改为UTF-8

    for line in open('keji.txt', 'r',encoding='UTF-8').readlines():
        print(line)
        corpus.append(line.strip())
    # print corpus
    time.sleep(5)

 

posted on 2020-03-20 09:30  一往无前!  阅读(1311)  评论(0编辑  收藏  举报