【问题标题】:wordcount: reducer python program throws ValueError字数:reducer python 程序抛出 ValueError
【发布时间】:2015-01-06 08:45:09
【问题描述】:

每当我尝试在 Hadoop 系统中运行 Reducer python 程序时,我都会收到此错误。 Mapper 程序虽然运行良好。已授予与我的 Mapper 程序相同的权限。有语法错误吗?

Traceback(最近一次调用最后一次): 文件“reducer.py”,第 13 行,在 word, count = line.split('\t', 1) ValueError: 需要超过 1 个值才能解压

            #!/usr/bin/env python
            import sys

            # maps words to their counts
            word2count = {}

            # input comes from STDIN
            for line in sys.stdin:
                # remove leading and trailing whitespace
                line = line.strip()

                # parse the input we got from mapper.py
                word, count = line.split('\t', 1)
                # convert count (currently a string) to int
                try:
                    count = int(count)
                except ValueError:
                    continue

                try:
                    word2count[word] = word2count[word]+count
                except:
                    word2count[word] = count

            # write the tuples to stdout
            # Note: they are unsorted
            for word in word2count.keys():
                print '%s\t%s'% ( word, word2count[word] )

【问题讨论】:

    标签: python hadoop


    【解决方案1】:

    我将 line.split('\t', 1) 更改为 line.split(' ', 1) 并且成功了。
    好像空格不太清楚,说清楚:应该是line.split('(one space here)', 1)

    【讨论】:

    • 好像空格不太清楚,应该是line.split('(这里一个空格)', 1)
    【解决方案2】:

    我无法详细回答。

    但是,当我删除了一些额外的print 时,我解决了同样的问题,我在mapper 中添加了一些print。可能与printsys.stdin 的作用有关。

    我知道你现在可能已经解决了这个问题

    【讨论】:

      【解决方案3】:

      错误ValueError: need more than 1 value to unpack 是当您在右侧使用太少的值进行多重赋值时引发的。所以看起来line 中没有\t,所以line.split('\t',1) 会产生一个值,从而导致类似于word, count = ("foo",) 的东西。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        • 2021-05-31
        • 1970-01-01
        • 1970-01-01
        • 2019-01-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多