问题 | UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 29解决办法

github:https://github.com/MichaelBeechan
CSDN:https://blog.csdn.net/u011344545

问题 | UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 29解决办法_第1张图片
python读文件:

 file = open(filename, "r") 
    for line in file:  #every line is a poem
        #print(line)
        title, poem = line.strip().split(":")  #get title and poem

出现UnicodeDecodeError:

 File "F:\MichaelProject\Chinese_poem\dataPretreatment.py", line 13, in pretreatment
    for line in file:  #every line is a poem
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 29: illegal multibyte sequence

修改:

file = open(filename, "r", encoding='UTF-8')# add encoding='UTF-8'
    for line in file:  #every line is a poem

按照上述修改可以使得程序成功运行

你可能感兴趣的:(python)