python 读取文件报错
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-6-0e9485397930> in <module>()
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: