深入Python - ValueError:需要多于1个值才能解压缩

时间:2017-11-18 15:11:18

标签: python valueerror

在Dive into Python的第6章中,作者将用于将单数英语名词改为复数形式的函数的规则放入单独的txt文件中:

[sxz]$ $ es 
[^aeioudgkprt]h$ $ es 
[^aeiou]y$ y$ ies 
$ $ s

然后在这里使用它:

import re

def rules(rules_filename):
    with open(rules_filename) as pattern_file:
        for line in pattern_file:
            pattern, search, replace = line.split(None, 3)
            yield build_match_and_apply_functions(pattern, search, replace)

def build_match_and_apply_functions(pattern, search, replace):
        def matches_rule(word):
            return re.search(pattern, word)
        def apply_rule(word):
            return re.sub(search, replace, word)
        return (matches_rule, apply_rule)

def plural(noun, rules_filename='plural.txt'):
        for matches_rule, apply_rule in rules(rules_filename):
                if matches_rule(noun):
                    return apply_rule(noun)
        raise ValueError('no matching rule for {0}'.format(noun))

我创建了一个txt文件,其中包含将单个法语名词更改为复数的规则(但使用逗号作为分隔符而不是空格):

[sxz]$,$,$
au$|\btravail\b|,au$|al$|ail,$aux
\bcorail\b|,au$|al$|ail,$aux
\bemail\b|,au$|al$|ail,$aux
\bvitrail\b|,au$|al$|ail,$aux
\banimal\b|,au$|al$|ail,$aux
\bjournal\b|,au$|al$|ail,$aux
\bcheval\b|,au$|al$|ail,$aux
\bhopital\b,au$|al$|ail,$aux
\bbijou\b|,ou$,oux
\bcaillou\b|,ou$,oux
\bchou\b|,ou$,oux
\bgenou\b|,ou$,oux
\bhibou\b|,ou$,oux
\bjoujou\b|,ou$,oux
\bpou\b,ou$,oux
eu$,$,eux
ou$,ou$,ous
$,$,s

现在我正在尝试在与作者相同的代码中使用此文件(我确实更改了文件名def plural(noun, rules_filename='rules.txt'),但我一直得到一个ValueError:

File "fr_pl4.py", line 10, in plural
    for matches_rule, apply_rule in rules(rules_filename):
File "fr_pl4.py", line 6, in rules
    pattern, search, replace = line.split(',', 3)
ValueError: need more than 1 value to unpack

我还使用了书中的规则和书中的代码(你可以在上面找到它们)...... 我仍然得到同样的错误:

>>>plural('chat') 
Traceback (most recent call last):
File "<stdin>", line 1, in <module> 
File "pl.py", line 10, in plural 
for matches_rule, apply_rule in rules(rules_filename): 
File "pl.py", line 6, in rules 
pattern, search, replace = line.split(None, 3) 
ValueError: need more than 1 value to unpack 

我就像在“plural5-rules.txt”一书中调用文件一样。

我尝试用空格替换逗号,但它仍然不起作用。另外,我的第二个问题是......我该怎么称呼这个功能?我是这样做的:

>>> from fr_pl4 import plural
>>> plural('chat')

非常感谢您提前回答我的问题!

更新:所以我按照 Evert 的建议行事,这就是我所得到的:

>>> plural('chat')
{\rtf1\ansi\ansicpg1252\cocoartf1504\cocoasubrtf830

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pl.py", line 11, in plural
    for matches_rule, apply_rule in rules(rules_filename):
  File "pl.py", line 7, in rules
    pattern, search, replace = line.split(None, 3)
ValueError: need more than 1 value to unpack

所以这可能是导致问题的原因!我使用了另一个文本编辑器,它终于工作了!!!!

>>> from pl import plural
>>> plural('cat')

[sxz]$ $ es

[^aeioudgkprt]h$ $ es

[^aeiou]y$ y$ ies

$ $ s 

'cats'

这是本书的规则。我不得不修改法语名词的规则,如下:

[sxz]$ $ $
au$ au$ aux
\btravail\b ail$ aux
\bcorail\b ail$ aux
\bemail\b ail$ aux
\bvitrail\b ail$ aux
\banimal\b al$ aux
\bjournal\b al$ aux
\bcheval\b al$ aux
\bhopital\b al$ aux
\bbijou\b ou$ oux
\bcaillou\b ou$ oux
\bchou\b ou$ oux
\bgenou\b ou$ oux
\bhibou\b ou$ oux
\bjoujou\b ou$ oux
\bpou\b ou$ oux
eu$ $ eux
ou$ ou$ ous
$ $ s

现在一切正常,谢谢你Evert !!!

0 个答案:

没有答案