3

这是代码:

import glob
import mincemeat
import re

text_files = glob.glob('finalcount/1/*')
def file_contents(file_name):
    f = open(file_name)
    try:
        return f.read()
    finally:
        f.close()

source = dict((file_name, file_contents(file_name))
          for file_name in text_files)

def mapfn(key, value):
    for line in value.splitlines():
        list1 = [ ]
        for temp in re.split('[\t]+',line):
            list1.append(temp)
        x = int(list1[1].strip());
        yield [list1[0],x]

def reducefn(key, value):
    return key, sum(value)

s = mincemeat.Server()
s.datasource = source
s.mapfn = mapfn
s.reducefn = reducefn

results = s.run_server(password="wola")
print results

此代码应该计算多个文件的字数。但它不断返回错误:

error: uncaptured python exception, closing channel <__main__.Client connected at 0x25c1990> 
(<type 'exceptions.ValueError'>:invalid literal for int() with base 10: '' 
 [C:\Python27\lib\asyncore.py|read|83] 
 [C:\Python27\lib\asyncore.py|handle_read_event|444] 
 [C:\Python27\lib\asynchat.py|handle_read|140] 
 [mincemeat.py|found_terminator|97] 
 [mincemeat.py|process_command|195] 
 [mincemeat.py|call_mapfn|171] 
 [projcount.py|mapfn|21])

我正在处理的输入文件如下所示。现在我想在不同的文件中添加单词并将它们旁边的数字相加。

fawn    24
gai 1
nunnery 11
sowell  3
sonja   29
woods   591
clotted 1
spiders 84
hanging 522

替换为 后re.splitline.split()出现此错误。

error: uncaptured python exception, closing channel <__main__.Client connected at 0x2531990> 
(<type 'exceptions.IndexError'>:list index out of range 
 [C:\Python27\lib\asyncore.py|read|83] 
 [C:\Python27\lib\asyncore.py|handle_read_event|444] 
 [C:\Python27\lib\asynchat.py|handle_read|140] 
 [mincemeat.py|found_terminator|97]
 [mincemeat.py|process_command|195] 
 [mincemeat.py|call_mapfn|171] 
 [projcount.py|mapfn|21]) 
4

1 回答 1

1

我在不同的场合遇到了这个错误,我发现问题是在你使用 python 3.3 时出现的,我删除了 3.3 并安装了 2.7.5(http://python.org/download/),它现在工作正常。:)

于 2013-09-25T12:18:18.157 回答