我无法在同一目录下的文件中找到词典

2024-06-09 01:26:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着写一篇关于地图缩小的作业。我在一个终端上运行:

ioannis@ioannis-desktop:~$ python hw3.py

然后在另一个终端:

^{pr2}$

hw3.py:

import mincemeat
import glob
from stopwords import allStopWords
text_files = glob.glob('/home/ioannis/Web Intelligence and Big Data/Week 3: Load - I/hw3data/hw3data/*')

def file_contents(file_name):
    f = open(file_name)
    try:     
        return f.read()
    except:
        print "exception!!!!!!"
    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():
            ........................
            ........................
            if word in allStopWords:
                continue        
            print(word)
        print(words_title)
        print("\n\n")

def reducefn(k, vs):
    result = sum(vs)
    return result

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

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

为什么不管用?您可以看到hw3.py和停止字.py在主目录中!在


Tags: nameinpyimport终端defglobfile
1条回答
网友
1楼 · 发布于 2024-06-09 01:26:56

One potential gotcha when using mincemeat.py: Your mapfn and reducefn functions don't have access to their enclosing environment, including imported modules. If you need to use an imported module in one of these functions, be sure to include import whatever in the functions themselves.

https://github.com/michaelfairley/mincemeatpy#imports

low:将from stopwords import allStopWords语句移到mapfn函数的顶部。在

相关问题 更多 >