这是代码:
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.split
,line.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])