'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence

python 读取文件报错

UnicodeDecodeError                        Traceback (most recent call last)
 in ()
      6 path = "data.txt"
      7 with open(path,'r') as file:
----> 8     for line in file:
      9         (itemId,userId,itemName,rating) = line.strip().split('\001')
     10         if('男' in itemName or '男女' in itemName):

UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence

解决办法

with open(path,'r' ) as file:

改为

with open(path,'r', encoding='UTF-8' ) as file:

你可能感兴趣的:(DataScience)