UnicodeDecodeError: 'gbk' codec can't decode byte 0xbc in position 21: illegal multibyte sequence Save the txt with UTF format and then open the file with f1 = open(path, encoding = 'utf')