AttributeError: ‘list‘ object has no attribute ‘decode‘

在做情感分类模型是,最初的数据预处理报错AttributeError: 'list' object has no attribute 'decode',源码如下:

import torch.nn as nn
import jieba

data_path = "data.csv"
data_list = open(data_path).readlines()[1:]

for item in data_list:
    label = item[0]
    content = item[2:].split()
    seg_list = jieba.cut(content,cut_all=False)
    for seg_item in seg_list:
        print(seg_item)

这里报错的原因是由于readlines()读取的数据是list类型,导致后面content的数据类型也为list;但在做jieba分词的时候,需要使用str类型,使用需要将content强制类型转换为str:

import torch.nn as nn
import jieba

data_path = "data.csv"
data_list = open(data_path).readlines()[1:]

for item in data_list:
    label = item[0]
    content = str(item[2:].split())
    seg_list = jieba.cut(content,cut_all=False)
    for seg_item in seg_list:
        print(seg_item)

程序报错如下:

AttributeError: ‘list‘ object has no attribute ‘decode‘_第1张图片

 

 

你可能感兴趣的:(python情感分类,中文分词,python)