/:pyhton中不支持的操作数类型:“ list”和“ int”

时间:2019-07-05 16:13:30

标签: python list int word2vec

我的代码有一些问题,他无法处理数据列表和int。 但是起初他能够在第27行上生成csv数据...但后来却无法... hasil = total / kata

有什么问题
import gensim
#import pandas as  pd
import re
import csv
import numpy as np
def processing(kata):
    words=re.sub(r'([^\s\w]|_)', '', kata)
    words= re.sub(r'[0-9]+', '', words)
    return words
def data():
    path = './model_terbaru/idwiki_word2vec_100.model'
    model = gensim.models.word2vec.Word2Vec.load(path)
    fp = open('data/data_train.csv', 'r')
    line = fp.readline()
    while line:
        processed = processing(line)
        print (processed)
        kata=len(processed.split())
        print(kata)
        total=[100]
        print(total)
        for word in processed.split():
            try:
                vector=model[""+word+""]
                print(vector)
                total=total+vector
                print(word)
            except:
                pass
        hasil=total/kata
        print(hasil)
        mylist= hasil 
        ok=open('data/vector_train.csv','a')
        a=csv.writer(ok,lineterminator='\n')
        a.writerows([mylist])
        ok.close()
        line = fp.readline()
    fp.close()

data()

hasil =总数/ kata TypeError:/:“ list”和“ int”

不支持的操作数类型

请帮助我。

1 个答案:

答案 0 :(得分:0)

 for word in processed.split():
    #for line in fp:
        try:
            vector=model[""+word+""]
            print(vector)
            total=total+vector
            print(word)
        except:
            pass
    if kata is not 0 :
        hasil = [x/kata for x in total]
    #hasil=total/kata
        print(hasil)
        mylist= hasil 
        ok=open('data/vector_train.csv','a')
        a=csv.writer(ok,lineterminator='\n')
        a.writerow([mylist])
        line = fp.readline()
        ok.close()
    else:
        print('tes')

现在我的代码是这样的,但是连续的单词被认为是0