Python - ValueError:要解压的值太多 - 为什么?

时间:2016-06-04 09:25:02

标签: python argv

我是Python的新手。我需要比较两个单词列表,并在一个列表中检测那些不在另一个列表中的单词。 这是两个测试文件

big_list.txt

[coin, co-operate, accurate, achieve, adapt, adjust, admire, admission, enter, advance, adventure, aeroplane, plane, affair, aim, objective, annual, approach, approve, argument]

small_list.txt

[coin, co-operate,  football, accurate, achieve, adapt, amazing, adjust, admire, admission, enter, advance, breakfast]

具有此预期输出

[football, amazing, breakfast] 

我这里有一个非常简单的Python脚本

from sys import argv
big_list, small_list = argv
blist = open(big_list).read()
slist = open(small_list).read()
dlist = [item for item in slist if item not in blist]
diff_list = open(dlist, 'w').write()
diff_list.close()

但运行时会返回此错误消息

roy@medea:~/e2tw/list_comparison$ python file_comp1.py big_list.txt small_list.txt
  Traceback (most recent call last):
       File "file_comp1.py", line 3, in <module>
          big_list, small_list = argv
   ValueError: too many values to unpack

4 个答案:

答案 0 :(得分:4)

尝试:

big_list, small_list = argv[1:]

为什么呢?因为默认情况下会将三个参数传递给您的脚本,而argv[0]是脚本名称

P.S。在最后两行中,有一个bug等待关闭。您无法传递列表作为对文件对象的引用。你应该这样做:

output_file = open("filename.txt", "w")
output_file.write("[%s]" % ", ".join(dlist))
output_file.close()

答案 1 :(得分:1)

argv[0]包含正在运行的python脚本的名称(类似于C的argv[0]具有可执行文件名的方式)。你显然不能将三个值(['file_comp1.py', 'big_list.txt', 'small_list.txt'])分成两个变量。例如,您可以将argv切片以仅获取第二个参数并继续:

big_list, small_list = argv[1:]

答案 2 :(得分:0)

至少尝试(请查看第二个代码段以获得真正完整的答案):

from sys import argv
big_list, small_list = argv[-2:]
blist = open(big_list).read()
slist = open(small_list).read()
dlist = [item for item in slist if item not in blist]
diff_list = open(dlist, 'w').write()
diff_list.close()

第一个条目将始终是您的脚本本身,但有许多事情不起作用,因为其他部分已经指出。看下面的工作代码:-)让你前进。

您还可以使用更广泛用于忽略索引0处的第一个条目的[1:]并完成所有其余操作。在hackish / rookie代码中,我更喜欢显式 - “预期的数量”参数。

但也许最好写这样的来开始:

#! /usr/bin/env python
from __future__ import print_function
import sys


def read_list_from_file(a_path):
    """Simple parser transforming a [a, b,] text in file
    at a_path into a list."""
    return [z.strip() for z in open(a_path, 'rt').read().strip('[]').split(',')]


def a_not_in_b_list(a_seq, b_seq):
    """Return the list of entries in a_seq but not in b_seq."""
    return [item for item in a_seq if item not in b_seq]


def main():
    """Drive the diff."""
    if len(sys.argv) == 3:
        big_list, small_list = sys.argv[1:]
    else:
        print("Usage:", __file__, "<big-list-file> <small-list-file>")
        return 2
    # Do something with the file names given here
    b_list = read_list_from_file(big_list)
    s_list = read_list_from_file(small_list)

    with open('diff_list.txt', 'w') as f:
        f.write('%s\n' % (repr(a_not_in_b_list(s_list, b_list)),))


if __name__ == '__main__':
    sys.exit(main())

在文本文件中运行此文件会在diff_list.txt中显示:

['football', 'amazing', 'breakfast']

答案 3 :(得分:0)

from sys import argv
big_list = argv[1]
small_list = argv[2]
blist = open(big_list).read()
slist = open(small_list).read()
dlist = [item for item in slist if item not in blist]
diff_list = open(dlist, 'w').write()
diff_list.close()

查看有关如何使用argv。Using argv

的答案

argv[0]是脚本名称。