python报错TypeError: must be str, not list

今天LDA话题聚类时,总是不能写入聚类结果,报错在下面绿色的这一行,TypeError: must be str, not list 

我试着像别人说的那样改open函数,还是报错,最后大神给出的方案成功跑通了

 

import re
import logging   # logbook
from gensim import corpora, models, similarities
import os
from pyltp import Segmentor, Postagger, Parser, NamedEntityRecognizer
import numpy as np
import time
from tqdm import tqdm

import sys, os
LTP_DATA_DIR = 'D:/LTP/ltp_data'  # ltp模型目录的路径
cws_model_path = os.path.join(LTP_DATA_DIR, 'cws.model')  # 分词模型路径,模型名称为`cws.model`
pos_model_path = os.path.join(LTP_DATA_DIR, 'pos.model')  # 词性标注模型路径,模型名称为`pos.model`
print("加载LTP模型... ...")

segmentor = Segmentor()  # 初始化实例
segmentor.load(cws_model_path)  # 加载模型

def load_lda(corpus,lda):
    # 主题推断
    for e, values in enumerate(lda.inference(corpus)[0]):
        print(texts[e])
        idx , val = 0, 0
        for ee, value in enumerate(values):
            print('\t主题%d推断值%.2f' % (ee, value))
            val = value if value >= val else val
            idx = ee if value >= val else idx
        print('\t主题%d推断值%.2f' % (idx, val))
        lei_f = open('lei_text'+ str(idx) +'.txt','a',encoding='utf-8')
   '''  lei_f.write(str(e)+'\t'+ str(idx)+'\t' + texts[e]) # 出错前的这一行'''  
        lei_f.write(str(e)+'\t'+ str(idx)+'\t' + '\t'.join(texts[e]))
        lei_f.write('\n')

 

 

你可能感兴趣的:(python报错TypeError: must be str, not list)