ValueError: too many values to unpack (expected 2)

原代码:

for line in file.readlines():  #every line is a poem
    #print(line)
    title, poem = line.strip().split(":") 

报错信息:

D:\anaconda\envs\tensorflow\python.exe E:/python/21tensorflow项目/word2vec/test1.py
Traceback (most recent call last):
  File "E:/python/21tensorflow项目/word2vec/test1.py", line 5, in 
    title, poem = line.strip().split(":")  #get title and poem
ValueError: too many values to unpack (expected 2)

Process finished with exit code 1

问题原因:

字典是一个迭代器对象,只支持Key的遍历,,如果想对key,value,则可以使用items方法。

解决后的代码如下:

for title,poem in filedic.items():

    title, poem = line.strip().split("::")  #get title and poem

 

你可能感兴趣的:(tensorflow,anaconda,自然语言处理,字典迭代)