Dictionary.py要解压的值太多了

时间:2013-12-16 12:38:41

标签: python

每当我尝试重新加载我的dictionary.DB文件时,它都会一直告诉我:

too many values to unpack (expected 4)

但是有4个值我正在使用word, name, definition, wordtime。工作时间仅为time.time()。这是def加载代码:

def load():
   dWord = {}
   for word in open("Dictionary.DB", "r").readlines():
      if len(word.strip()) >0:
         word, name, definition, wordtime = word.split()
         dWord[word] = name, definition, int(wordtime)
   Dictionary.dWord = dWord

# Adding words to DB
Dictionary.dWord[word] = user, definition, wordtime

示例:

{'dog': ('charles', 'a animal', 1387199008)}

打印word.split()时,结果为:

['dog', 'charles', 'a', 'animal', '1387199870']

2 个答案:

答案 0 :(得分:0)

看起来像是这条线:

word, name, definition, wordtime = word.split()

是问题所在。检查 word.split()返回的内容。根据您发布的错误,它必须返回超过四个值。也许是一些空字符串。

答案 1 :(得分:0)

你可以这样做:

raw = word.split()
word, name, definition, wordtime = raw[0], raw[1], ' '.join(raw[2:-1]), raw[-1]

如果您确定空格只出现在definition

这适用于更长的不同空格数:

word = 'dog charles a cute cute animal 1387199870'
# definition -> 'a cute cute animal'