一个python错误 - 太多的值

时间:2012-03-08 07:35:51

标签: python

我正试图从标记文件中提取专有名词。但问题是我正在尝试的代码有时会出现错误:

Traceback (most recent call last):
File "E:\pt\paragraph", line 35, in <module>
sen1= noun(mylist[s])
File "E:\pt\paragraph", line 5, in noun
word, tag = word.split('/')
ValueError: too many values to unpack

代码适用于某些文本,但有些文本会出错。

代码:

def noun(words):
nouns = []
for word in words.split():
word, tag = word.split('/')
  if (tag.lower() == 'np'):
    nouns.append(word);
return nouns

def splitParagraph(paragraph):

import re
paragraphs = paragraph.split('\n\n')
return paragraphs

if __name__ == '__main__':
import nltk
rp = open("t3.txt", 'r')
text = rp.read()
mylist = []
para = splitParagraph(text)

for s in para:
mylist.append(s)
for s in range(len(mylist)-1):
sen1= noun(mylist[s])
sen2= noun(mylist[s+1])

目前我正在努力工作,如果我删除第一段其他方面它给出了错误。

案文示例:

A/at good/jj man/nn-hl departs/vbz-hl ./. Goodbye/uh-hl ,/,-hl Mr./np-hl Sam/np-hl./. Sam/np Rayburn/np was/bedz a/at good/jj man/nn ,/, a/at good/jj American/np ,/, and/cc ,/, third/od ,/, a/at good/jj Democrat/np ./. He/pps was/bedz all/abn of/in these/dts rolled/vbn into/in one/cd sturdy/jj figure/nn ;/. ;/. Mr./np Speaker/nn-tl ,/, Mr./np Sam/np ,/, and/cc Mr./np Democrat/np ,/, at/in one/cd and/cc the/at same/ap time/nn ./.

The/at House/nn-tl was/bedz his/pp$ habitat/nn and/cc there/rb he/pps flourished/vbd ,/, first/rb as/cs a/at young/jj representative/nn ,/, then/rb as/cs a/at forceful/jj committee/nn chairman/nn ,/, and/cc finally/rb in/in the/at post/nn for/in which/wdt he/pps seemed/vbd intended/vbn from/in birth/nn ,/, Speaker/nn-tl of/in-tl the/at-tl House/nn-tl ,/, and/cc second/od most/ql powerful/jj man/nn in/in Washington/np ./.

如果我删除第1段(A / at good / jj man / nn-hl离开......)代码可以正常工作。如何解决这个问题。

提前感谢。

3 个答案:

答案 0 :(得分:1)

你的“单词”包含多个“/”。 所以解压缩它(标记,单词)将无法正常工作。您必须弄清楚如何处理标签/单词有多个“/".

的情况
def noun(words):
    nouns = []
    for word in words.split():
        items = word.split('/')
        if len(items) == 2:
            tag, word = items
        else:
            # do something else to parse

    ....

我刚刚意识到,如果你只想在上分开“/”,就可以使用“maxsplit”选项来分割字符串。

>>> word = "a/b/c"
>>>
>>> word.split("/", 1)
['a', 'b/c']

答案 1 :(得分:0)

您在'/'处拆分并尝试获得2个值。但是你有一个“单词”,它有多个'/'!:

Sam/np-hl./. 

所以你得到['Sam','np-hl。','。']你试图只分配给两个变量。

答案 2 :(得分:0)

错误意味着split()方法返回的次数超过了“解包”到=符号左侧的变量数量。

以下是一个例子:

>>> x,y = 'a,b,c,d'.split(',')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
>>> x,y,z,t = 'a,b,c,d'.split(',')

那对您来说意味着word包含两对以上的tags

问题是这个“对”:Sam/np-hl./.没有空格,所以当你拆分这对时,你实际得到['Sam','np-hl','.'],这就是导致错误的原因。