ValueError:使用泡菜时没有足够的值可解压

时间:2019-05-13 14:22:20

标签: python-3.x dictionary nlp load pickle

我试图查看其他问题,但找不到答案。我不明白为什么会有这个错误:

 File "lexicon_based.py", line 50, in <module>
    for word, (pos, neu, neg) in zip(pickle.load(fd)):
ValueError: not enough values to unpack (expected 2, got 1)

代码中似乎出现错误的部分:

with open("dict_pickle", "rb") as fd:
    dico_lexique = {}
    for word, (pos, neu, neg) in zip(pickle.load(fd)):
        dico_lexique[word] = Sentiment(int(pos), int(neu), int(neg))

创建对象“ dict_pickle”的代码,初始文件至少包含400000行:

with codecs.open('lexiconSentiment.txt', 'r', 'Cp1252', errors = 'ignore') as text_file:
    text_file =(text_file.read())
    #print(text_file)

dico_lexique =  ({i.split(";")[1]:i.split(";")[2:] for i in text_file.split("\n") if i}) # Spliting the text content and creating a dictionary
#print(dico_lexique)
#print(type(dico_lexique))

pickle_out = open("dict_pickle", "wb")
pickle.dump(dico_lexique, pickle_out)
pickle_out.close()

使用pickle之前字典文件的外观:

0;***;21;127;0
1;vraiment;407;156;37
2;oiseau-à-berceau;102;259;0
3;Stoph;95;308;37
4;Conscience;372;144;35
5;rançonnement;0;635;433
6;provenir;304;227;47
7;féliciteur;285;54;1
8;traversée;360;167;38
9;avant toute chose;241;108;34
10;Porcellis;52;276;0
11;Lasker-Schüler;146;284;0
12;discréditer;0;47;866
13;adjuration;300;44;40
14;Besnier-Boeck-Schaumann;0;39;315

1 个答案:

答案 0 :(得分:0)

似乎您需要pickle.load(fd).items()而不是zip(pickle.load(fd))zip()旨在与可迭代对象的可迭代对象一起使用,而不适用于字典。