如何解决ValueError:需要多于1个值才能解压缩?

时间:2018-03-01 15:52:06

标签: python count

我使用python 2.7,我试着找到我的文件中的每个POS如何发生我写这段代码:

os.chdir()

,输出如下:

     from collections import Counter
     f = open("POS.txt","r")
     text = f.read()
     print(text)
     counts = Counter(tag for word,tag in text)
     print(counts)

如何解决此错误?这是我文件中的输入:

      File "C:/Python27/Lib/countPOS.py", line 10, in <genexpr>
      counts = Counter(tag for word,tag in text)
      ValueError: need more than 1 value to unpack

1 个答案:

答案 0 :(得分:2)

您遇到此问题,因为从文件中读取数据后,变量“text”类型为字符串。您需要转换数据类型。

以下是工作代码:

from collections import Counter
import ast

f = open("POS.txt", "r")
text = f.read()
# print(type(text)) returns string


text = ast.literal_eval(text)
# print (type(text)) returns list

counts = Counter(tag for word, tag in text)
print(counts)