|
Python中读取txt文本出现 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence
报错原因是读取的文件中有中文。
def load_data(filename):
D = []
with open(filename,'r') as f:
for i, l in enumerate(f):
l = json.loads(l)
text, label = l['sentence'], l['label']
D.append((text, labels.index(label)))
return D
改成:
def load_data(filename):
D = []
with open(filename,'r', encoding='utf-8') as f:
for i, l in enumerate(f):
l = json.loads(l)
text, label = l['sentence'], l['label']
D.append((text, labels.index(label)))
return D
|
|